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