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