ipoib_multicast.c 24.7 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 37 38 39 40 41 42 43
 *
 * 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>
#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 45
#include <net/dst.h>

L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55
#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

56
static DEFINE_MUTEX(mcast_mutex);
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

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;
};

static void ipoib_mcast_free(struct ipoib_mcast *mcast)
{
	struct net_device *dev = mcast->dev;
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ipoib_neigh *neigh, *tmp;
	unsigned long flags;
73
	int tx_dropped = 0;
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81

	ipoib_dbg_mcast(netdev_priv(dev),
			"deleting multicast group " IPOIB_GID_FMT "\n",
			IPOIB_GID_ARG(mcast->mcmember.mgid));

	spin_lock_irqsave(&priv->lock, flags);

	list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
82 83 84 85 86 87
		/*
		 * It's safe to call ipoib_put_ah() inside priv->lock
		 * here, because we know that mcast->ah will always
		 * hold one more reference, so ipoib_put_ah() will
		 * never do more than decrement the ref count.
		 */
L
Linus Torvalds 已提交
88
		if (neigh->ah)
89
			ipoib_put_ah(neigh->ah);
90
		ipoib_neigh_free(dev, neigh);
L
Linus Torvalds 已提交
91 92 93 94 95 96 97
	}

	spin_unlock_irqrestore(&priv->lock, flags);

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

98 99
	while (!skb_queue_empty(&mcast->pkt_queue)) {
		++tx_dropped;
100
		dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
101 102 103
	}

	spin_lock_irqsave(&priv->tx_lock, flags);
104
	dev->stats.tx_dropped += tx_dropped;
105
	spin_unlock_irqrestore(&priv->tx_lock, flags);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114

	kfree(mcast);
}

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

R
Roland Dreier 已提交
115
	mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
L
Linus Torvalds 已提交
116 117 118 119 120
	if (!mcast)
		return NULL;

	mcast->dev = dev;
	mcast->created = jiffies;
121
	mcast->backoff = 1;
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129

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

	return mcast;
}

130
static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139 140
{
	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);

141
		ret = memcmp(mgid, mcast->mcmember.mgid.raw,
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
			     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);
187
	struct ipoib_ah *ah;
L
Linus Torvalds 已提交
188
	int ret;
189
	int set_qkey = 0;
L
Linus Torvalds 已提交
190 191 192 193 194 195

	mcast->mcmember = *mcmember;

	/* Set the cached Q_Key before we attach if it's the broadcast group */
	if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
		    sizeof (union ib_gid))) {
196 197 198 199 200
		spin_lock_irq(&priv->lock);
		if (!priv->broadcast) {
			spin_unlock_irq(&priv->lock);
			return -EAGAIN;
		}
L
Linus Torvalds 已提交
201
		priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
202
		spin_unlock_irq(&priv->lock);
L
Linus Torvalds 已提交
203
		priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
204
		set_qkey = 1;
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216
	}

	if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
		if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
			ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
				   " already attached\n",
				   IPOIB_GID_ARG(mcast->mcmember.mgid));

			return 0;
		}

		ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
217
					 &mcast->mcmember.mgid, set_qkey);
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		if (ret < 0) {
			ipoib_warn(priv, "couldn't attach QP to multicast group "
				   IPOIB_GID_FMT "\n",
				   IPOIB_GID_ARG(mcast->mcmember.mgid));

			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,
234
			.static_rate   = mcast->mcmember.rate,
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243
			.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;

244 245
		ah = ipoib_create_ah(dev, priv->pd, &av);
		if (!ah) {
L
Linus Torvalds 已提交
246 247
			ipoib_warn(priv, "ib_address_create failed\n");
		} else {
248 249 250 251
			spin_lock_irq(&priv->lock);
			mcast->ah = ah;
			spin_unlock_irq(&priv->lock);

L
Linus Torvalds 已提交
252 253 254 255 256 257 258 259 260 261
			ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
					" AV %p, LID 0x%04x, SL %d\n",
					IPOIB_GID_ARG(mcast->mcmember.mgid),
					mcast->ah->ah,
					be16_to_cpu(mcast->mcmember.mlid),
					mcast->mcmember.sl);
		}
	}

	/* actually send any queued packets */
262
	spin_lock_irq(&priv->tx_lock);
L
Linus Torvalds 已提交
263 264
	while (!skb_queue_empty(&mcast->pkt_queue)) {
		struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
265
		spin_unlock_irq(&priv->tx_lock);
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273 274 275

		skb->dev = dev;

		if (!skb->dst || !skb->dst->neighbour) {
			/* put pseudoheader back on for next time */
			skb_push(skb, sizeof (struct ipoib_pseudoheader));
		}

		if (dev_queue_xmit(skb))
			ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
276
		spin_lock_irq(&priv->tx_lock);
L
Linus Torvalds 已提交
277
	}
278
	spin_unlock_irq(&priv->tx_lock);
L
Linus Torvalds 已提交
279 280 281 282

	return 0;
}

283
static int
L
Linus Torvalds 已提交
284
ipoib_mcast_sendonly_join_complete(int status,
285
				   struct ib_sa_multicast *multicast)
L
Linus Torvalds 已提交
286
{
287
	struct ipoib_mcast *mcast = multicast->context;
L
Linus Torvalds 已提交
288
	struct net_device *dev = mcast->dev;
289
	struct ipoib_dev_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
290

291 292 293 294
	/* We trap for port events ourselves. */
	if (status == -ENETRESET)
		return 0;

L
Linus Torvalds 已提交
295
	if (!status)
296 297 298
		status = ipoib_mcast_join_finish(mcast, &multicast->rec);

	if (status) {
L
Linus Torvalds 已提交
299 300 301 302 303 304
		if (mcast->logcount++ < 20)
			ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
					IPOIB_GID_FMT ", status %d\n",
					IPOIB_GID_ARG(mcast->mcmember.mgid), status);

		/* Flush out any queued packets */
305 306
		spin_lock_irq(&priv->tx_lock);
		while (!skb_queue_empty(&mcast->pkt_queue)) {
307
			++dev->stats.tx_dropped;
308
			dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
309 310
		}
		spin_unlock_irq(&priv->tx_lock);
L
Linus Torvalds 已提交
311 312

		/* Clear the busy flag so we try again */
313 314
		status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
					    &mcast->flags);
L
Linus Torvalds 已提交
315
	}
316
	return status;
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
}

static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
{
	struct net_device *dev = mcast->dev;
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ib_sa_mcmember_rec rec = {
#if 0				/* Some SMs don't support send-only yet */
		.join_state = 4
#else
		.join_state = 1
#endif
	};
	int ret = 0;

	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
		ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
		return -ENODEV;
	}

	if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
		ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
		return -EBUSY;
	}

	rec.mgid     = mcast->mcmember.mgid;
	rec.port_gid = priv->local_gid;
344
	rec.pkey     = cpu_to_be16(priv->pkey);
L
Linus Torvalds 已提交
345

346 347 348 349 350 351 352 353 354 355 356 357 358
	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
					 priv->port, &rec,
					 IB_SA_MCMEMBER_REC_MGID	|
					 IB_SA_MCMEMBER_REC_PORT_GID	|
					 IB_SA_MCMEMBER_REC_PKEY	|
					 IB_SA_MCMEMBER_REC_JOIN_STATE,
					 GFP_ATOMIC,
					 ipoib_mcast_sendonly_join_complete,
					 mcast);
	if (IS_ERR(mcast->mc)) {
		ret = PTR_ERR(mcast->mc);
		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
		ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
L
Linus Torvalds 已提交
359 360 361 362 363 364 365 366 367 368
			   ret);
	} else {
		ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
				", starting join\n",
				IPOIB_GID_ARG(mcast->mcmember.mgid));
	}

	return ret;
}

369 370
static int ipoib_mcast_join_complete(int status,
				     struct ib_sa_multicast *multicast)
L
Linus Torvalds 已提交
371
{
372
	struct ipoib_mcast *mcast = multicast->context;
L
Linus Torvalds 已提交
373 374 375 376 377 378 379
	struct net_device *dev = mcast->dev;
	struct ipoib_dev_priv *priv = netdev_priv(dev);

	ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
			" (status %d)\n",
			IPOIB_GID_ARG(mcast->mcmember.mgid), status);

380 381 382 383 384 385 386 387
	/* We trap for port events ourselves. */
	if (status == -ENETRESET)
		return 0;

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

	if (!status) {
388
		mcast->backoff = 1;
389
		mutex_lock(&mcast_mutex);
L
Linus Torvalds 已提交
390
		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
D
David Howells 已提交
391 392
			queue_delayed_work(ipoib_workqueue,
					   &priv->mcast_task, 0);
393
		mutex_unlock(&mcast_mutex);
394

395 396 397 398 399 400 401
		if (mcast == priv->broadcast) {
			/*
			 * Take RTNL lock here to avoid racing with
			 * ipoib_stop() and turning the carrier back
			 * on while a device is being removed.
			 */
			rtnl_lock();
402
			netif_carrier_on(dev);
403 404
			rtnl_unlock();
		}
405

406
		return 0;
L
Linus Torvalds 已提交
407 408
	}

409 410
	if (mcast->logcount++ < 20) {
		if (status == -ETIMEDOUT) {
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
			ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
					", status %d\n",
					IPOIB_GID_ARG(mcast->mcmember.mgid),
					status);
		} else {
			ipoib_warn(priv, "multicast join failed for "
				   IPOIB_GID_FMT ", status %d\n",
				   IPOIB_GID_ARG(mcast->mcmember.mgid),
				   status);
		}
	}

	mcast->backoff *= 2;
	if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
		mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;

427 428
	/* Clear the busy flag so we try again */
	status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
429

430
	mutex_lock(&mcast_mutex);
431
	spin_lock_irq(&priv->lock);
432 433 434
	if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
		queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
				   mcast->backoff * HZ);
435
	spin_unlock_irq(&priv->lock);
436
	mutex_unlock(&mcast_mutex);
L
Linus Torvalds 已提交
437

438
	return status;
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
}

static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
			     int create)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ib_sa_mcmember_rec rec = {
		.join_state = 1
	};
	ib_sa_comp_mask comp_mask;
	int ret = 0;

	ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
			IPOIB_GID_ARG(mcast->mcmember.mgid));

	rec.mgid     = mcast->mcmember.mgid;
	rec.port_gid = priv->local_gid;
456
	rec.pkey     = cpu_to_be16(priv->pkey);
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464 465

	comp_mask =
		IB_SA_MCMEMBER_REC_MGID		|
		IB_SA_MCMEMBER_REC_PORT_GID	|
		IB_SA_MCMEMBER_REC_PKEY		|
		IB_SA_MCMEMBER_REC_JOIN_STATE;

	if (create) {
		comp_mask |=
466 467 468 469 470 471 472 473 474
			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 已提交
475 476

		rec.qkey	  = priv->broadcast->mcmember.qkey;
477 478 479 480 481
		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 已提交
482 483
		rec.sl		  = priv->broadcast->mcmember.sl;
		rec.flow_label	  = priv->broadcast->mcmember.flow_label;
484
		rec.hop_limit	  = priv->broadcast->mcmember.hop_limit;
L
Linus Torvalds 已提交
485 486
	}

487 488 489 490 491 492 493 494
	set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
					 &rec, comp_mask, GFP_KERNEL,
					 ipoib_mcast_join_complete, mcast);
	if (IS_ERR(mcast->mc)) {
		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
		ret = PTR_ERR(mcast->mc);
		ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
L
Linus Torvalds 已提交
495 496 497 498 499

		mcast->backoff *= 2;
		if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
			mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;

500
		mutex_lock(&mcast_mutex);
L
Linus Torvalds 已提交
501 502 503
		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
			queue_delayed_work(ipoib_workqueue,
					   &priv->mcast_task,
504
					   mcast->backoff * HZ);
505
		mutex_unlock(&mcast_mutex);
506
	}
L
Linus Torvalds 已提交
507 508
}

D
David Howells 已提交
509
void ipoib_mcast_join_task(struct work_struct *work)
L
Linus Torvalds 已提交
510
{
D
David Howells 已提交
511 512 513
	struct ipoib_dev_priv *priv =
		container_of(work, struct ipoib_dev_priv, mcast_task.work);
	struct net_device *dev = priv->dev;
L
Linus Torvalds 已提交
514 515 516 517 518

	if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
		return;

	if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
519
		ipoib_warn(priv, "ib_query_gid() failed\n");
L
Linus Torvalds 已提交
520 521 522 523 524 525
	else
		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));

	{
		struct ib_port_attr attr;

526 527 528
		if (!ib_query_port(priv->ca, priv->port, &attr))
			priv->local_lid = attr.lid;
		else
529
			ipoib_warn(priv, "ib_query_port failed\n");
L
Linus Torvalds 已提交
530 531 532
	}

	if (!priv->broadcast) {
533 534 535 536
		struct ipoib_mcast *broadcast;

		broadcast = ipoib_mcast_alloc(dev, 1);
		if (!broadcast) {
L
Linus Torvalds 已提交
537
			ipoib_warn(priv, "failed to allocate broadcast group\n");
538
			mutex_lock(&mcast_mutex);
L
Linus Torvalds 已提交
539 540 541
			if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
				queue_delayed_work(ipoib_workqueue,
						   &priv->mcast_task, HZ);
542
			mutex_unlock(&mcast_mutex);
L
Linus Torvalds 已提交
543 544 545
			return;
		}

546 547
		spin_lock_irq(&priv->lock);
		memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
L
Linus Torvalds 已提交
548
		       sizeof (union ib_gid));
549
		priv->broadcast = broadcast;
L
Linus Torvalds 已提交
550 551 552 553 554 555

		__ipoib_mcast_add(dev, priv->broadcast);
		spin_unlock_irq(&priv->lock);
	}

	if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
556 557
		if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
			ipoib_mcast_join(dev, priv->broadcast, 0);
L
Linus Torvalds 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
		return;
	}

	while (1) {
		struct ipoib_mcast *mcast = NULL;

		spin_lock_irq(&priv->lock);
		list_for_each_entry(mcast, &priv->multicast_list, list) {
			if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
			    && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
			    && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
				/* Found the next unjoined group */
				break;
			}
		}
		spin_unlock_irq(&priv->lock);

		if (&mcast->list == &priv->multicast_list) {
			/* All done */
			break;
		}

		ipoib_mcast_join(dev, mcast, 1);
		return;
	}

584
	priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
585

586 587
	if (!ipoib_cm_admin_enabled(dev)) {
		rtnl_lock();
588
		dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
589 590
		rtnl_unlock();
	}
L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599 600 601 602

	ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");

	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
}

int ipoib_mcast_start_thread(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

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

603
	mutex_lock(&mcast_mutex);
L
Linus Torvalds 已提交
604
	if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
D
David Howells 已提交
605
		queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
606
	mutex_unlock(&mcast_mutex);
L
Linus Torvalds 已提交
607 608 609 610

	return 0;
}

611
int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
L
Linus Torvalds 已提交
612 613 614 615 616
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

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

617
	mutex_lock(&mcast_mutex);
L
Linus Torvalds 已提交
618 619
	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
	cancel_delayed_work(&priv->mcast_task);
620
	mutex_unlock(&mcast_mutex);
L
Linus Torvalds 已提交
621

622 623
	if (flush)
		flush_workqueue(ipoib_workqueue);
L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631 632

	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;

633 634 635
	if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
		ib_sa_free_multicast(mcast->mc);

636 637 638
	if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
		ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
				IPOIB_GID_ARG(mcast->mcmember.mgid));
L
Linus Torvalds 已提交
639

640
		/* Remove ourselves from the multicast group */
641 642
		ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
				      be16_to_cpu(mcast->mcmember.mlid));
643
		if (ret)
644
			ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
645
	}
L
Linus Torvalds 已提交
646 647 648 649

	return 0;
}

650
void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ipoib_mcast *mcast;

	/*
	 * We can only be called from ipoib_start_xmit, so we're
	 * inside tx_lock -- no need to save/restore flags.
	 */
	spin_lock(&priv->lock);

661
	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)		||
662 663
	    !priv->broadcast					||
	    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
664
		++dev->stats.tx_dropped;
665 666 667 668
		dev_kfree_skb_any(skb);
		goto unlock;
	}

L
Linus Torvalds 已提交
669 670 671 672
	mcast = __ipoib_mcast_find(dev, mgid);
	if (!mcast) {
		/* Let's create a new send only group now */
		ipoib_dbg_mcast(priv, "setting up send only multicast group for "
673
				IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
L
Linus Torvalds 已提交
674 675 676 677 678

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

		set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
685
		memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
L
Linus Torvalds 已提交
686 687 688 689 690 691 692
		__ipoib_mcast_add(dev, mcast);
		list_add_tail(&mcast->list, &priv->multicast_list);
	}

	if (!mcast->ah) {
		if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
			skb_queue_tail(&mcast->pkt_queue, skb);
693
		else {
694
			++dev->stats.tx_dropped;
L
Linus Torvalds 已提交
695
			dev_kfree_skb_any(skb);
696
		}
L
Linus Torvalds 已提交
697

698
		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
L
Linus Torvalds 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712
			ipoib_dbg_mcast(priv, "no address vector, "
					"but multicast join already started\n");
		else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
			ipoib_mcast_sendonly_join(mcast);

		/*
		 * If lookup completes between here and out:, don't
		 * want to send packet twice.
		 */
		mcast = NULL;
	}

out:
	if (mcast && mcast->ah) {
713
		if (skb->dst		&&
L
Linus Torvalds 已提交
714 715
		    skb->dst->neighbour &&
		    !*to_ipoib_neigh(skb->dst->neighbour)) {
716 717
			struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
									skb->dev);
L
Linus Torvalds 已提交
718 719 720

			if (neigh) {
				kref_get(&mcast->ah->ref);
721
				neigh->ah	= mcast->ah;
L
Linus Torvalds 已提交
722 723 724 725 726 727 728
				list_add_tail(&neigh->list, &mcast->neigh_list);
			}
		}

		ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
	}

729
unlock:
L
Linus Torvalds 已提交
730 731 732 733 734 735 736
	spin_unlock(&priv->lock);
}

void ipoib_mcast_dev_flush(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	LIST_HEAD(remove_list);
737
	struct ipoib_mcast *mcast, *tmcast;
L
Linus Torvalds 已提交
738 739 740 741 742 743
	unsigned long flags;

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

	spin_lock_irqsave(&priv->lock, flags);

744 745 746 747
	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 已提交
748 749 750
	}

	if (priv->broadcast) {
R
Roland Dreier 已提交
751
		rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
752 753
		list_add_tail(&priv->broadcast->list, &remove_list);
		priv->broadcast = NULL;
L
Linus Torvalds 已提交
754 755 756 757 758 759 760 761 762 763
	}

	spin_unlock_irqrestore(&priv->lock, flags);

	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
		ipoib_mcast_leave(dev, mcast);
		ipoib_mcast_free(mcast);
	}
}

D
David Howells 已提交
764
void ipoib_mcast_restart_task(struct work_struct *work)
L
Linus Torvalds 已提交
765
{
D
David Howells 已提交
766 767 768
	struct ipoib_dev_priv *priv =
		container_of(work, struct ipoib_dev_priv, restart_task);
	struct net_device *dev = priv->dev;
L
Linus Torvalds 已提交
769 770 771 772
	struct dev_mc_list *mclist;
	struct ipoib_mcast *mcast, *tmcast;
	LIST_HEAD(remove_list);
	unsigned long flags;
773
	struct ib_sa_mcmember_rec rec;
L
Linus Torvalds 已提交
774 775 776

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

777
	ipoib_mcast_stop_thread(dev, 0);
L
Linus Torvalds 已提交
778

H
Herbert Xu 已提交
779
	local_irq_save(flags);
780
	netif_addr_lock(dev);
781
	spin_lock(&priv->lock);
L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

	/*
	 * 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 */
	for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
		union ib_gid mgid;

		memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);

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

803 804 805 806 807 808 809 810
			/* 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)) {
				ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
						IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
				continue;
			}

L
Linus Torvalds 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
			/* Not found or send-only group, let's add a new entry */
			ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
					IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));

			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 已提交
827
				list_move_tail(&mcast->list, &remove_list);
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

				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)) {
			ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
					IPOIB_GID_ARG(mcast->mcmember.mgid));

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

			/* Move to the remove list */
A
Akinobu Mita 已提交
852
			list_move_tail(&mcast->list, &remove_list);
L
Linus Torvalds 已提交
853 854
		}
	}
855 856

	spin_unlock(&priv->lock);
857
	netif_addr_unlock(dev);
H
Herbert Xu 已提交
858
	local_irq_restore(flags);
L
Linus Torvalds 已提交
859 860 861 862 863 864 865 866 867 868 869

	/* We have to cancel outside of the spinlock */
	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
		ipoib_mcast_leave(mcast->dev, mcast);
		ipoib_mcast_free(mcast);
	}

	if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
		ipoib_mcast_start_thread(dev);
}

870 871
#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG

L
Linus Torvalds 已提交
872 873 874 875 876 877 878 879 880
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;
881
	memset(iter->mgid.raw, 0, 16);
L
Linus Torvalds 已提交
882 883

	if (ipoib_mcast_iter_next(iter)) {
884
		kfree(iter);
L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
		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;
}
939 940

#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */