• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/moduleparam.h>
38 #include <linux/ip.h>
39 #include <linux/in.h>
40 #include <linux/igmp.h>
41 #include <linux/inetdevice.h>
42 #include <linux/delay.h>
43 #include <linux/completion.h>
44 #include <linux/slab.h>
45 
46 #include <net/dst.h>
47 
48 #include "ipoib.h"
49 
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51 static int mcast_debug_level;
52 
53 module_param(mcast_debug_level, int, 0644);
54 MODULE_PARM_DESC(mcast_debug_level,
55 		 "Enable multicast debug tracing if > 0");
56 #endif
57 
58 static DEFINE_MUTEX(mcast_mutex);
59 
60 struct ipoib_mcast_iter {
61 	struct net_device *dev;
62 	union ib_gid       mgid;
63 	unsigned long      created;
64 	unsigned int       queuelen;
65 	unsigned int       complete;
66 	unsigned int       send_only;
67 };
68 
ipoib_mcast_free(struct ipoib_mcast * mcast)69 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
70 {
71 	struct net_device *dev = mcast->dev;
72 	struct ipoib_dev_priv *priv = netdev_priv(dev);
73 	struct ipoib_neigh *neigh, *tmp;
74 	int tx_dropped = 0;
75 
76 	ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
77 			mcast->mcmember.mgid.raw);
78 
79 	spin_lock_irq(&priv->lock);
80 
81 	list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
82 		/*
83 		 * It's safe to call ipoib_put_ah() inside priv->lock
84 		 * here, because we know that mcast->ah will always
85 		 * hold one more reference, so ipoib_put_ah() will
86 		 * never do more than decrement the ref count.
87 		 */
88 		if (neigh->ah)
89 			ipoib_put_ah(neigh->ah);
90 		ipoib_neigh_free(dev, neigh);
91 	}
92 
93 	spin_unlock_irq(&priv->lock);
94 
95 	if (mcast->ah)
96 		ipoib_put_ah(mcast->ah);
97 
98 	while (!skb_queue_empty(&mcast->pkt_queue)) {
99 		++tx_dropped;
100 		dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
101 	}
102 
103 	netif_tx_lock_bh(dev);
104 	dev->stats.tx_dropped += tx_dropped;
105 	netif_tx_unlock_bh(dev);
106 
107 	kfree(mcast);
108 }
109 
ipoib_mcast_alloc(struct net_device * dev,int can_sleep)110 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
111 					     int can_sleep)
112 {
113 	struct ipoib_mcast *mcast;
114 
115 	mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
116 	if (!mcast)
117 		return NULL;
118 
119 	mcast->dev = dev;
120 	mcast->created = jiffies;
121 	mcast->backoff = 1;
122 
123 	INIT_LIST_HEAD(&mcast->list);
124 	INIT_LIST_HEAD(&mcast->neigh_list);
125 	skb_queue_head_init(&mcast->pkt_queue);
126 
127 	return mcast;
128 }
129 
__ipoib_mcast_find(struct net_device * dev,void * mgid)130 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
131 {
132 	struct ipoib_dev_priv *priv = netdev_priv(dev);
133 	struct rb_node *n = priv->multicast_tree.rb_node;
134 
135 	while (n) {
136 		struct ipoib_mcast *mcast;
137 		int ret;
138 
139 		mcast = rb_entry(n, struct ipoib_mcast, rb_node);
140 
141 		ret = memcmp(mgid, mcast->mcmember.mgid.raw,
142 			     sizeof (union ib_gid));
143 		if (ret < 0)
144 			n = n->rb_left;
145 		else if (ret > 0)
146 			n = n->rb_right;
147 		else
148 			return mcast;
149 	}
150 
151 	return NULL;
152 }
153 
__ipoib_mcast_add(struct net_device * dev,struct ipoib_mcast * mcast)154 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
155 {
156 	struct ipoib_dev_priv *priv = netdev_priv(dev);
157 	struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
158 
159 	while (*n) {
160 		struct ipoib_mcast *tmcast;
161 		int ret;
162 
163 		pn = *n;
164 		tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
165 
166 		ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
167 			     sizeof (union ib_gid));
168 		if (ret < 0)
169 			n = &pn->rb_left;
170 		else if (ret > 0)
171 			n = &pn->rb_right;
172 		else
173 			return -EEXIST;
174 	}
175 
176 	rb_link_node(&mcast->rb_node, pn, n);
177 	rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
178 
179 	return 0;
180 }
181 
ipoib_mcast_join_finish(struct ipoib_mcast * mcast,struct ib_sa_mcmember_rec * mcmember)182 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
183 				   struct ib_sa_mcmember_rec *mcmember)
184 {
185 	struct net_device *dev = mcast->dev;
186 	struct ipoib_dev_priv *priv = netdev_priv(dev);
187 	struct ipoib_ah *ah;
188 	int ret;
189 	int set_qkey = 0;
190 
191 	mcast->mcmember = *mcmember;
192 
193 	/* Set the multicast MTU and cached Q_Key before we attach if it's
194 	 * the broadcast group.
195 	 */
196 	if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
197 		    sizeof (union ib_gid))) {
198 		spin_lock_irq(&priv->lock);
199 		if (!priv->broadcast) {
200 			spin_unlock_irq(&priv->lock);
201 			return -EAGAIN;
202 		}
203 		priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
204 		priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
205 		spin_unlock_irq(&priv->lock);
206 		priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
207 		set_qkey = 1;
208 
209 		if (!ipoib_cm_admin_enabled(dev)) {
210 			rtnl_lock();
211 			dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
212 			rtnl_unlock();
213 		}
214 	}
215 
216 	if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
217 		if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
218 			ipoib_warn(priv, "multicast group %pI6 already attached\n",
219 				   mcast->mcmember.mgid.raw);
220 
221 			return 0;
222 		}
223 
224 		ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
225 					 &mcast->mcmember.mgid, set_qkey);
226 		if (ret < 0) {
227 			ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
228 				   mcast->mcmember.mgid.raw);
229 
230 			clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
231 			return ret;
232 		}
233 	}
234 
235 	{
236 		struct ib_ah_attr av = {
237 			.dlid	       = be16_to_cpu(mcast->mcmember.mlid),
238 			.port_num      = priv->port,
239 			.sl	       = mcast->mcmember.sl,
240 			.ah_flags      = IB_AH_GRH,
241 			.static_rate   = mcast->mcmember.rate,
242 			.grh	       = {
243 				.flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
244 				.hop_limit     = mcast->mcmember.hop_limit,
245 				.sgid_index    = 0,
246 				.traffic_class = mcast->mcmember.traffic_class
247 			}
248 		};
249 		av.grh.dgid = mcast->mcmember.mgid;
250 
251 		ah = ipoib_create_ah(dev, priv->pd, &av);
252 		if (IS_ERR(ah)) {
253 			ipoib_warn(priv, "ib_address_create failed %ld\n",
254 				-PTR_ERR(ah));
255 			/* use original error */
256 			return PTR_ERR(ah);
257 		} else {
258 			spin_lock_irq(&priv->lock);
259 			mcast->ah = ah;
260 			spin_unlock_irq(&priv->lock);
261 
262 			ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
263 					mcast->mcmember.mgid.raw,
264 					mcast->ah->ah,
265 					be16_to_cpu(mcast->mcmember.mlid),
266 					mcast->mcmember.sl);
267 		}
268 	}
269 
270 	/* actually send any queued packets */
271 	netif_tx_lock_bh(dev);
272 	while (!skb_queue_empty(&mcast->pkt_queue)) {
273 		struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
274 
275 		netif_tx_unlock_bh(dev);
276 
277 		skb->dev = dev;
278 		if (dev_queue_xmit(skb))
279 			ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
280 
281 		netif_tx_lock_bh(dev);
282 	}
283 	netif_tx_unlock_bh(dev);
284 
285 	return 0;
286 }
287 
288 static int
ipoib_mcast_sendonly_join_complete(int status,struct ib_sa_multicast * multicast)289 ipoib_mcast_sendonly_join_complete(int status,
290 				   struct ib_sa_multicast *multicast)
291 {
292 	struct ipoib_mcast *mcast = multicast->context;
293 	struct net_device *dev = mcast->dev;
294 
295 	/* We trap for port events ourselves. */
296 	if (status == -ENETRESET)
297 		return 0;
298 
299 	if (!status)
300 		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
301 
302 	if (status) {
303 		if (mcast->logcount++ < 20)
304 			ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
305 					mcast->mcmember.mgid.raw, status);
306 
307 		/* Flush out any queued packets */
308 		netif_tx_lock_bh(dev);
309 		while (!skb_queue_empty(&mcast->pkt_queue)) {
310 			++dev->stats.tx_dropped;
311 			dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
312 		}
313 		netif_tx_unlock_bh(dev);
314 
315 		/* Clear the busy flag so we try again */
316 		status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
317 					    &mcast->flags);
318 	}
319 	return status;
320 }
321 
ipoib_mcast_sendonly_join(struct ipoib_mcast * mcast)322 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
323 {
324 	struct net_device *dev = mcast->dev;
325 	struct ipoib_dev_priv *priv = netdev_priv(dev);
326 	struct ib_sa_mcmember_rec rec = {
327 #if 0				/* Some SMs don't support send-only yet */
328 		.join_state = 4
329 #else
330 		.join_state = 1
331 #endif
332 	};
333 	int ret = 0;
334 
335 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
336 		ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
337 		return -ENODEV;
338 	}
339 
340 	if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
341 		ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
342 		return -EBUSY;
343 	}
344 
345 	rec.mgid     = mcast->mcmember.mgid;
346 	rec.port_gid = priv->local_gid;
347 	rec.pkey     = cpu_to_be16(priv->pkey);
348 
349 	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
350 					 priv->port, &rec,
351 					 IB_SA_MCMEMBER_REC_MGID	|
352 					 IB_SA_MCMEMBER_REC_PORT_GID	|
353 					 IB_SA_MCMEMBER_REC_PKEY	|
354 					 IB_SA_MCMEMBER_REC_JOIN_STATE,
355 					 GFP_ATOMIC,
356 					 ipoib_mcast_sendonly_join_complete,
357 					 mcast);
358 	if (IS_ERR(mcast->mc)) {
359 		ret = PTR_ERR(mcast->mc);
360 		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
361 		ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
362 			   ret);
363 	} else {
364 		ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
365 				mcast->mcmember.mgid.raw);
366 	}
367 
368 	return ret;
369 }
370 
ipoib_mcast_carrier_on_task(struct work_struct * work)371 void ipoib_mcast_carrier_on_task(struct work_struct *work)
372 {
373 	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
374 						   carrier_on_task);
375 	struct ib_port_attr attr;
376 
377 	/*
378 	 * Take rtnl_lock to avoid racing with ipoib_stop() and
379 	 * turning the carrier back on while a device is being
380 	 * removed.
381 	 */
382 	if (ib_query_port(priv->ca, priv->port, &attr) ||
383 	    attr.state != IB_PORT_ACTIVE) {
384 		ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
385 		return;
386 	}
387 
388 	rtnl_lock();
389 	netif_carrier_on(priv->dev);
390 	rtnl_unlock();
391 }
392 
ipoib_mcast_join_complete(int status,struct ib_sa_multicast * multicast)393 static int ipoib_mcast_join_complete(int status,
394 				     struct ib_sa_multicast *multicast)
395 {
396 	struct ipoib_mcast *mcast = multicast->context;
397 	struct net_device *dev = mcast->dev;
398 	struct ipoib_dev_priv *priv = netdev_priv(dev);
399 
400 	ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
401 			mcast->mcmember.mgid.raw, status);
402 
403 	/* We trap for port events ourselves. */
404 	if (status == -ENETRESET)
405 		return 0;
406 
407 	if (!status)
408 		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
409 
410 	if (!status) {
411 		mcast->backoff = 1;
412 		mutex_lock(&mcast_mutex);
413 		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
414 			queue_delayed_work(ipoib_workqueue,
415 					   &priv->mcast_task, 0);
416 		mutex_unlock(&mcast_mutex);
417 
418 		/*
419 		 * Defer carrier on work to ipoib_workqueue to avoid a
420 		 * deadlock on rtnl_lock here.
421 		 */
422 		if (mcast == priv->broadcast)
423 			queue_work(ipoib_workqueue, &priv->carrier_on_task);
424 
425 		return 0;
426 	}
427 
428 	if (mcast->logcount++ < 20) {
429 		if (status == -ETIMEDOUT || status == -EAGAIN) {
430 			ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
431 					mcast->mcmember.mgid.raw, status);
432 		} else {
433 			ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
434 				   mcast->mcmember.mgid.raw, status);
435 		}
436 	}
437 
438 	mcast->backoff *= 2;
439 	if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
440 		mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
441 
442 	/* Clear the busy flag so we try again */
443 	status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
444 
445 	mutex_lock(&mcast_mutex);
446 	spin_lock_irq(&priv->lock);
447 	if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
448 		queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
449 				   mcast->backoff * HZ);
450 	spin_unlock_irq(&priv->lock);
451 	mutex_unlock(&mcast_mutex);
452 
453 	return status;
454 }
455 
ipoib_mcast_join(struct net_device * dev,struct ipoib_mcast * mcast,int create)456 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
457 			     int create)
458 {
459 	struct ipoib_dev_priv *priv = netdev_priv(dev);
460 	struct ib_sa_mcmember_rec rec = {
461 		.join_state = 1
462 	};
463 	ib_sa_comp_mask comp_mask;
464 	int ret = 0;
465 
466 	ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
467 
468 	rec.mgid     = mcast->mcmember.mgid;
469 	rec.port_gid = priv->local_gid;
470 	rec.pkey     = cpu_to_be16(priv->pkey);
471 
472 	comp_mask =
473 		IB_SA_MCMEMBER_REC_MGID		|
474 		IB_SA_MCMEMBER_REC_PORT_GID	|
475 		IB_SA_MCMEMBER_REC_PKEY		|
476 		IB_SA_MCMEMBER_REC_JOIN_STATE;
477 
478 	if (create) {
479 		comp_mask |=
480 			IB_SA_MCMEMBER_REC_QKEY			|
481 			IB_SA_MCMEMBER_REC_MTU_SELECTOR		|
482 			IB_SA_MCMEMBER_REC_MTU			|
483 			IB_SA_MCMEMBER_REC_TRAFFIC_CLASS	|
484 			IB_SA_MCMEMBER_REC_RATE_SELECTOR	|
485 			IB_SA_MCMEMBER_REC_RATE			|
486 			IB_SA_MCMEMBER_REC_SL			|
487 			IB_SA_MCMEMBER_REC_FLOW_LABEL		|
488 			IB_SA_MCMEMBER_REC_HOP_LIMIT;
489 
490 		rec.qkey	  = priv->broadcast->mcmember.qkey;
491 		rec.mtu_selector  = IB_SA_EQ;
492 		rec.mtu		  = priv->broadcast->mcmember.mtu;
493 		rec.traffic_class = priv->broadcast->mcmember.traffic_class;
494 		rec.rate_selector = IB_SA_EQ;
495 		rec.rate	  = priv->broadcast->mcmember.rate;
496 		rec.sl		  = priv->broadcast->mcmember.sl;
497 		rec.flow_label	  = priv->broadcast->mcmember.flow_label;
498 		rec.hop_limit	  = priv->broadcast->mcmember.hop_limit;
499 	}
500 
501 	set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
502 	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
503 					 &rec, comp_mask, GFP_KERNEL,
504 					 ipoib_mcast_join_complete, mcast);
505 	if (IS_ERR(mcast->mc)) {
506 		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
507 		ret = PTR_ERR(mcast->mc);
508 		ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
509 
510 		mcast->backoff *= 2;
511 		if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
512 			mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
513 
514 		mutex_lock(&mcast_mutex);
515 		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
516 			queue_delayed_work(ipoib_workqueue,
517 					   &priv->mcast_task,
518 					   mcast->backoff * HZ);
519 		mutex_unlock(&mcast_mutex);
520 	}
521 }
522 
ipoib_mcast_join_task(struct work_struct * work)523 void ipoib_mcast_join_task(struct work_struct *work)
524 {
525 	struct ipoib_dev_priv *priv =
526 		container_of(work, struct ipoib_dev_priv, mcast_task.work);
527 	struct net_device *dev = priv->dev;
528 
529 	if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
530 		return;
531 
532 	if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
533 		ipoib_warn(priv, "ib_query_gid() failed\n");
534 	else
535 		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
536 
537 	{
538 		struct ib_port_attr attr;
539 
540 		if (!ib_query_port(priv->ca, priv->port, &attr))
541 			priv->local_lid = attr.lid;
542 		else
543 			ipoib_warn(priv, "ib_query_port failed\n");
544 	}
545 
546 	if (!priv->broadcast) {
547 		struct ipoib_mcast *broadcast;
548 
549 		if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
550 			return;
551 
552 		broadcast = ipoib_mcast_alloc(dev, 1);
553 		if (!broadcast) {
554 			ipoib_warn(priv, "failed to allocate broadcast group\n");
555 			mutex_lock(&mcast_mutex);
556 			if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
557 				queue_delayed_work(ipoib_workqueue,
558 						   &priv->mcast_task, HZ);
559 			mutex_unlock(&mcast_mutex);
560 			return;
561 		}
562 
563 		spin_lock_irq(&priv->lock);
564 		memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
565 		       sizeof (union ib_gid));
566 		priv->broadcast = broadcast;
567 
568 		__ipoib_mcast_add(dev, priv->broadcast);
569 		spin_unlock_irq(&priv->lock);
570 	}
571 
572 	if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
573 		if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
574 			ipoib_mcast_join(dev, priv->broadcast, 0);
575 		return;
576 	}
577 
578 	while (1) {
579 		struct ipoib_mcast *mcast = NULL;
580 
581 		spin_lock_irq(&priv->lock);
582 		list_for_each_entry(mcast, &priv->multicast_list, list) {
583 			if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
584 			    && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
585 			    && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
586 				/* Found the next unjoined group */
587 				break;
588 			}
589 		}
590 		spin_unlock_irq(&priv->lock);
591 
592 		if (&mcast->list == &priv->multicast_list) {
593 			/* All done */
594 			break;
595 		}
596 
597 		ipoib_mcast_join(dev, mcast, 1);
598 		return;
599 	}
600 
601 	ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
602 
603 	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
604 }
605 
ipoib_mcast_start_thread(struct net_device * dev)606 int ipoib_mcast_start_thread(struct net_device *dev)
607 {
608 	struct ipoib_dev_priv *priv = netdev_priv(dev);
609 
610 	ipoib_dbg_mcast(priv, "starting multicast thread\n");
611 
612 	mutex_lock(&mcast_mutex);
613 	if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
614 		queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
615 	mutex_unlock(&mcast_mutex);
616 
617 	return 0;
618 }
619 
ipoib_mcast_stop_thread(struct net_device * dev,int flush)620 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
621 {
622 	struct ipoib_dev_priv *priv = netdev_priv(dev);
623 
624 	ipoib_dbg_mcast(priv, "stopping multicast thread\n");
625 
626 	mutex_lock(&mcast_mutex);
627 	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
628 	cancel_delayed_work(&priv->mcast_task);
629 	mutex_unlock(&mcast_mutex);
630 
631 	if (flush)
632 		flush_workqueue(ipoib_workqueue);
633 
634 	return 0;
635 }
636 
ipoib_mcast_leave(struct net_device * dev,struct ipoib_mcast * mcast)637 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
638 {
639 	struct ipoib_dev_priv *priv = netdev_priv(dev);
640 	int ret = 0;
641 
642 	if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
643 		ib_sa_free_multicast(mcast->mc);
644 
645 	if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
646 		ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
647 				mcast->mcmember.mgid.raw);
648 
649 		/* Remove ourselves from the multicast group */
650 		ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
651 				      be16_to_cpu(mcast->mcmember.mlid));
652 		if (ret)
653 			ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
654 	}
655 
656 	return 0;
657 }
658 
ipoib_mcast_send(struct net_device * dev,void * mgid,struct sk_buff * skb)659 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
660 {
661 	struct ipoib_dev_priv *priv = netdev_priv(dev);
662 	struct ipoib_mcast *mcast;
663 	unsigned long flags;
664 
665 	spin_lock_irqsave(&priv->lock, flags);
666 
667 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)		||
668 	    !priv->broadcast					||
669 	    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
670 		++dev->stats.tx_dropped;
671 		dev_kfree_skb_any(skb);
672 		goto unlock;
673 	}
674 
675 	mcast = __ipoib_mcast_find(dev, mgid);
676 	if (!mcast) {
677 		/* Let's create a new send only group now */
678 		ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
679 				mgid);
680 
681 		mcast = ipoib_mcast_alloc(dev, 0);
682 		if (!mcast) {
683 			ipoib_warn(priv, "unable to allocate memory for "
684 				   "multicast structure\n");
685 			++dev->stats.tx_dropped;
686 			dev_kfree_skb_any(skb);
687 			goto out;
688 		}
689 
690 		set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
691 		memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
692 		__ipoib_mcast_add(dev, mcast);
693 		list_add_tail(&mcast->list, &priv->multicast_list);
694 	}
695 
696 	if (!mcast->ah) {
697 		if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
698 			skb_queue_tail(&mcast->pkt_queue, skb);
699 		else {
700 			++dev->stats.tx_dropped;
701 			dev_kfree_skb_any(skb);
702 		}
703 
704 		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
705 			ipoib_dbg_mcast(priv, "no address vector, "
706 					"but multicast join already started\n");
707 		else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
708 			ipoib_mcast_sendonly_join(mcast);
709 
710 		/*
711 		 * If lookup completes between here and out:, don't
712 		 * want to send packet twice.
713 		 */
714 		mcast = NULL;
715 	}
716 
717 out:
718 	if (mcast && mcast->ah) {
719 		struct dst_entry *dst = skb_dst(skb);
720 		struct neighbour *n = NULL;
721 
722 		rcu_read_lock();
723 		if (dst)
724 			n = dst_get_neighbour_noref(dst);
725 		if (n && !*to_ipoib_neigh(n)) {
726 			struct ipoib_neigh *neigh = ipoib_neigh_alloc(n,
727 								      skb->dev);
728 
729 			if (neigh) {
730 				kref_get(&mcast->ah->ref);
731 				neigh->ah	= mcast->ah;
732 				list_add_tail(&neigh->list, &mcast->neigh_list);
733 			}
734 		}
735 		rcu_read_unlock();
736 		spin_unlock_irqrestore(&priv->lock, flags);
737 		ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
738 		return;
739 	}
740 
741 unlock:
742 	spin_unlock_irqrestore(&priv->lock, flags);
743 }
744 
ipoib_mcast_dev_flush(struct net_device * dev)745 void ipoib_mcast_dev_flush(struct net_device *dev)
746 {
747 	struct ipoib_dev_priv *priv = netdev_priv(dev);
748 	LIST_HEAD(remove_list);
749 	struct ipoib_mcast *mcast, *tmcast;
750 	unsigned long flags;
751 
752 	ipoib_dbg_mcast(priv, "flushing multicast list\n");
753 
754 	spin_lock_irqsave(&priv->lock, flags);
755 
756 	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
757 		list_del(&mcast->list);
758 		rb_erase(&mcast->rb_node, &priv->multicast_tree);
759 		list_add_tail(&mcast->list, &remove_list);
760 	}
761 
762 	if (priv->broadcast) {
763 		rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
764 		list_add_tail(&priv->broadcast->list, &remove_list);
765 		priv->broadcast = NULL;
766 	}
767 
768 	spin_unlock_irqrestore(&priv->lock, flags);
769 
770 	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
771 		ipoib_mcast_leave(dev, mcast);
772 		ipoib_mcast_free(mcast);
773 	}
774 }
775 
ipoib_mcast_addr_is_valid(const u8 * addr,const u8 * broadcast)776 static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
777 {
778 	/* reserved QPN, prefix, scope */
779 	if (memcmp(addr, broadcast, 6))
780 		return 0;
781 	/* signature lower, pkey */
782 	if (memcmp(addr + 7, broadcast + 7, 3))
783 		return 0;
784 	return 1;
785 }
786 
ipoib_mcast_restart_task(struct work_struct * work)787 void ipoib_mcast_restart_task(struct work_struct *work)
788 {
789 	struct ipoib_dev_priv *priv =
790 		container_of(work, struct ipoib_dev_priv, restart_task);
791 	struct net_device *dev = priv->dev;
792 	struct netdev_hw_addr *ha;
793 	struct ipoib_mcast *mcast, *tmcast;
794 	LIST_HEAD(remove_list);
795 	unsigned long flags;
796 	struct ib_sa_mcmember_rec rec;
797 
798 	ipoib_dbg_mcast(priv, "restarting multicast task\n");
799 
800 	ipoib_mcast_stop_thread(dev, 0);
801 
802 	local_irq_save(flags);
803 	netif_addr_lock(dev);
804 	spin_lock(&priv->lock);
805 
806 	/*
807 	 * Unfortunately, the networking core only gives us a list of all of
808 	 * the multicast hardware addresses. We need to figure out which ones
809 	 * are new and which ones have been removed
810 	 */
811 
812 	/* Clear out the found flag */
813 	list_for_each_entry(mcast, &priv->multicast_list, list)
814 		clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
815 
816 	/* Mark all of the entries that are found or don't exist */
817 	netdev_for_each_mc_addr(ha, dev) {
818 		union ib_gid mgid;
819 
820 		if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
821 			continue;
822 
823 		memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
824 
825 		mcast = __ipoib_mcast_find(dev, &mgid);
826 		if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
827 			struct ipoib_mcast *nmcast;
828 
829 			/* ignore group which is directly joined by userspace */
830 			if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
831 			    !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
832 				ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
833 						mgid.raw);
834 				continue;
835 			}
836 
837 			/* Not found or send-only group, let's add a new entry */
838 			ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
839 					mgid.raw);
840 
841 			nmcast = ipoib_mcast_alloc(dev, 0);
842 			if (!nmcast) {
843 				ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
844 				continue;
845 			}
846 
847 			set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
848 
849 			nmcast->mcmember.mgid = mgid;
850 
851 			if (mcast) {
852 				/* Destroy the send only entry */
853 				list_move_tail(&mcast->list, &remove_list);
854 
855 				rb_replace_node(&mcast->rb_node,
856 						&nmcast->rb_node,
857 						&priv->multicast_tree);
858 			} else
859 				__ipoib_mcast_add(dev, nmcast);
860 
861 			list_add_tail(&nmcast->list, &priv->multicast_list);
862 		}
863 
864 		if (mcast)
865 			set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
866 	}
867 
868 	/* Remove all of the entries don't exist anymore */
869 	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
870 		if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
871 		    !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
872 			ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
873 					mcast->mcmember.mgid.raw);
874 
875 			rb_erase(&mcast->rb_node, &priv->multicast_tree);
876 
877 			/* Move to the remove list */
878 			list_move_tail(&mcast->list, &remove_list);
879 		}
880 	}
881 
882 	spin_unlock(&priv->lock);
883 	netif_addr_unlock(dev);
884 	local_irq_restore(flags);
885 
886 	/* We have to cancel outside of the spinlock */
887 	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
888 		ipoib_mcast_leave(mcast->dev, mcast);
889 		ipoib_mcast_free(mcast);
890 	}
891 
892 	if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
893 		ipoib_mcast_start_thread(dev);
894 }
895 
896 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
897 
ipoib_mcast_iter_init(struct net_device * dev)898 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
899 {
900 	struct ipoib_mcast_iter *iter;
901 
902 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
903 	if (!iter)
904 		return NULL;
905 
906 	iter->dev = dev;
907 	memset(iter->mgid.raw, 0, 16);
908 
909 	if (ipoib_mcast_iter_next(iter)) {
910 		kfree(iter);
911 		return NULL;
912 	}
913 
914 	return iter;
915 }
916 
ipoib_mcast_iter_next(struct ipoib_mcast_iter * iter)917 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
918 {
919 	struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
920 	struct rb_node *n;
921 	struct ipoib_mcast *mcast;
922 	int ret = 1;
923 
924 	spin_lock_irq(&priv->lock);
925 
926 	n = rb_first(&priv->multicast_tree);
927 
928 	while (n) {
929 		mcast = rb_entry(n, struct ipoib_mcast, rb_node);
930 
931 		if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
932 			   sizeof (union ib_gid)) < 0) {
933 			iter->mgid      = mcast->mcmember.mgid;
934 			iter->created   = mcast->created;
935 			iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
936 			iter->complete  = !!mcast->ah;
937 			iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
938 
939 			ret = 0;
940 
941 			break;
942 		}
943 
944 		n = rb_next(n);
945 	}
946 
947 	spin_unlock_irq(&priv->lock);
948 
949 	return ret;
950 }
951 
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)952 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
953 			   union ib_gid *mgid,
954 			   unsigned long *created,
955 			   unsigned int *queuelen,
956 			   unsigned int *complete,
957 			   unsigned int *send_only)
958 {
959 	*mgid      = iter->mgid;
960 	*created   = iter->created;
961 	*queuelen  = iter->queuelen;
962 	*complete  = iter->complete;
963 	*send_only = iter->send_only;
964 }
965 
966 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
967