2 * Copyright (c) 2015, Mellanox Technologies inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include "core_priv.h"
36 #include <linux/in6.h>
38 /* For in6_dev_get/in6_dev_put */
39 #include <net/addrconf.h>
40 #include <net/bonding.h>
42 #include <rdma/ib_cache.h>
43 #include <rdma/ib_addr.h>
50 struct update_gid_event_work
{
51 struct work_struct work
;
53 struct ib_gid_attr gid_attr
;
54 enum gid_op_type gid_op
;
57 #define ROCE_NETDEV_CALLBACK_SZ 3
58 struct netdev_event_work_cmd
{
59 roce_netdev_callback cb
;
60 roce_netdev_filter filter
;
61 struct net_device
*ndev
;
62 struct net_device
*filter_ndev
;
65 struct netdev_event_work
{
66 struct work_struct work
;
67 struct netdev_event_work_cmd cmds
[ROCE_NETDEV_CALLBACK_SZ
];
71 bool (*is_supported
)(const struct ib_device
*device
, u8 port_num
);
72 enum ib_gid_type gid_type
;
73 } PORT_CAP_TO_GID_TYPE
[] = {
74 {rdma_protocol_roce_eth_encap
, IB_GID_TYPE_ROCE
},
75 {rdma_protocol_roce_udp_encap
, IB_GID_TYPE_ROCE_UDP_ENCAP
},
78 #define CAP_TO_GID_TABLE_SIZE ARRAY_SIZE(PORT_CAP_TO_GID_TYPE)
80 unsigned long roce_gid_type_mask_support(struct ib_device
*ib_dev
, u8 port
)
83 unsigned int ret_flags
= 0;
85 if (!rdma_protocol_roce(ib_dev
, port
))
86 return 1UL << IB_GID_TYPE_IB
;
88 for (i
= 0; i
< CAP_TO_GID_TABLE_SIZE
; i
++)
89 if (PORT_CAP_TO_GID_TYPE
[i
].is_supported(ib_dev
, port
))
90 ret_flags
|= 1UL << PORT_CAP_TO_GID_TYPE
[i
].gid_type
;
94 EXPORT_SYMBOL(roce_gid_type_mask_support
);
96 static void update_gid(enum gid_op_type gid_op
, struct ib_device
*ib_dev
,
97 u8 port
, union ib_gid
*gid
,
98 struct ib_gid_attr
*gid_attr
)
101 unsigned long gid_type_mask
= roce_gid_type_mask_support(ib_dev
, port
);
103 for (i
= 0; i
< IB_GID_TYPE_SIZE
; i
++) {
104 if ((1UL << i
) & gid_type_mask
) {
105 gid_attr
->gid_type
= i
;
108 ib_cache_gid_add(ib_dev
, port
,
112 ib_cache_gid_del(ib_dev
, port
,
120 enum bonding_slave_state
{
121 BONDING_SLAVE_STATE_ACTIVE
= 1UL << 0,
122 BONDING_SLAVE_STATE_INACTIVE
= 1UL << 1,
123 /* No primary slave or the device isn't a slave in bonding */
124 BONDING_SLAVE_STATE_NA
= 1UL << 2,
127 static enum bonding_slave_state
is_eth_active_slave_of_bonding_rcu(struct net_device
*dev
,
128 struct net_device
*upper
)
130 if (upper
&& netif_is_bond_master(upper
)) {
131 struct net_device
*pdev
=
132 bond_option_active_slave_get_rcu(netdev_priv(upper
));
135 return dev
== pdev
? BONDING_SLAVE_STATE_ACTIVE
:
136 BONDING_SLAVE_STATE_INACTIVE
;
139 return BONDING_SLAVE_STATE_NA
;
142 #define REQUIRED_BOND_STATES (BONDING_SLAVE_STATE_ACTIVE | \
143 BONDING_SLAVE_STATE_NA)
144 static int is_eth_port_of_netdev(struct ib_device
*ib_dev
, u8 port
,
145 struct net_device
*rdma_ndev
, void *cookie
)
147 struct net_device
*real_dev
;
154 real_dev
= rdma_vlan_dev_real_dev(cookie
);
158 res
= ((rdma_is_upper_dev_rcu(rdma_ndev
, cookie
) &&
159 (is_eth_active_slave_of_bonding_rcu(rdma_ndev
, real_dev
) &
160 REQUIRED_BOND_STATES
)) ||
161 real_dev
== rdma_ndev
);
167 static int is_eth_port_inactive_slave(struct ib_device
*ib_dev
, u8 port
,
168 struct net_device
*rdma_ndev
, void *cookie
)
170 struct net_device
*master_dev
;
177 master_dev
= netdev_master_upper_dev_get_rcu(rdma_ndev
);
178 res
= is_eth_active_slave_of_bonding_rcu(rdma_ndev
, master_dev
) ==
179 BONDING_SLAVE_STATE_INACTIVE
;
185 static int pass_all_filter(struct ib_device
*ib_dev
, u8 port
,
186 struct net_device
*rdma_ndev
, void *cookie
)
191 static int upper_device_filter(struct ib_device
*ib_dev
, u8 port
,
192 struct net_device
*rdma_ndev
, void *cookie
)
199 if (rdma_ndev
== cookie
)
203 res
= rdma_is_upper_dev_rcu(rdma_ndev
, cookie
);
209 static void update_gid_ip(enum gid_op_type gid_op
,
210 struct ib_device
*ib_dev
,
211 u8 port
, struct net_device
*ndev
,
212 struct sockaddr
*addr
)
215 struct ib_gid_attr gid_attr
;
217 rdma_ip2gid(addr
, &gid
);
218 memset(&gid_attr
, 0, sizeof(gid_attr
));
219 gid_attr
.ndev
= ndev
;
221 update_gid(gid_op
, ib_dev
, port
, &gid
, &gid_attr
);
224 static void enum_netdev_default_gids(struct ib_device
*ib_dev
,
225 u8 port
, struct net_device
*event_ndev
,
226 struct net_device
*rdma_ndev
)
228 unsigned long gid_type_mask
;
232 ((rdma_ndev
!= event_ndev
&&
233 !rdma_is_upper_dev_rcu(rdma_ndev
, event_ndev
)) ||
234 is_eth_active_slave_of_bonding_rcu(rdma_ndev
,
235 netdev_master_upper_dev_get_rcu(rdma_ndev
)) ==
236 BONDING_SLAVE_STATE_INACTIVE
)) {
242 gid_type_mask
= roce_gid_type_mask_support(ib_dev
, port
);
244 ib_cache_gid_set_default_gid(ib_dev
, port
, rdma_ndev
, gid_type_mask
,
245 IB_CACHE_GID_DEFAULT_MODE_SET
);
248 static void bond_delete_netdev_default_gids(struct ib_device
*ib_dev
,
250 struct net_device
*event_ndev
,
251 struct net_device
*rdma_ndev
)
253 struct net_device
*real_dev
= rdma_vlan_dev_real_dev(event_ndev
);
259 real_dev
= event_ndev
;
263 if (rdma_is_upper_dev_rcu(rdma_ndev
, event_ndev
) &&
264 is_eth_active_slave_of_bonding_rcu(rdma_ndev
, real_dev
) ==
265 BONDING_SLAVE_STATE_INACTIVE
) {
266 unsigned long gid_type_mask
;
270 gid_type_mask
= roce_gid_type_mask_support(ib_dev
, port
);
272 ib_cache_gid_set_default_gid(ib_dev
, port
, rdma_ndev
,
274 IB_CACHE_GID_DEFAULT_MODE_DELETE
);
280 static void enum_netdev_ipv4_ips(struct ib_device
*ib_dev
,
281 u8 port
, struct net_device
*ndev
)
283 struct in_device
*in_dev
;
285 struct list_head list
;
286 struct sockaddr_in ip
;
288 struct sin_list
*sin_iter
;
289 struct sin_list
*sin_temp
;
292 if (ndev
->reg_state
>= NETREG_UNREGISTERING
)
296 in_dev
= __in_dev_get_rcu(ndev
);
303 struct sin_list
*entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
308 entry
->ip
.sin_family
= AF_INET
;
309 entry
->ip
.sin_addr
.s_addr
= ifa
->ifa_address
;
310 list_add_tail(&entry
->list
, &sin_list
);
315 list_for_each_entry_safe(sin_iter
, sin_temp
, &sin_list
, list
) {
316 update_gid_ip(GID_ADD
, ib_dev
, port
, ndev
,
317 (struct sockaddr
*)&sin_iter
->ip
);
318 list_del(&sin_iter
->list
);
323 static void enum_netdev_ipv6_ips(struct ib_device
*ib_dev
,
324 u8 port
, struct net_device
*ndev
)
326 struct inet6_ifaddr
*ifp
;
327 struct inet6_dev
*in6_dev
;
329 struct list_head list
;
330 struct sockaddr_in6 sin6
;
332 struct sin6_list
*sin6_iter
;
333 struct sin6_list
*sin6_temp
;
334 struct ib_gid_attr gid_attr
= {.ndev
= ndev
};
335 LIST_HEAD(sin6_list
);
337 if (ndev
->reg_state
>= NETREG_UNREGISTERING
)
340 in6_dev
= in6_dev_get(ndev
);
344 read_lock_bh(&in6_dev
->lock
);
345 list_for_each_entry(ifp
, &in6_dev
->addr_list
, if_list
) {
346 struct sin6_list
*entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
351 entry
->sin6
.sin6_family
= AF_INET6
;
352 entry
->sin6
.sin6_addr
= ifp
->addr
;
353 list_add_tail(&entry
->list
, &sin6_list
);
355 read_unlock_bh(&in6_dev
->lock
);
357 in6_dev_put(in6_dev
);
359 list_for_each_entry_safe(sin6_iter
, sin6_temp
, &sin6_list
, list
) {
362 rdma_ip2gid((struct sockaddr
*)&sin6_iter
->sin6
, &gid
);
363 update_gid(GID_ADD
, ib_dev
, port
, &gid
, &gid_attr
);
364 list_del(&sin6_iter
->list
);
369 static void _add_netdev_ips(struct ib_device
*ib_dev
, u8 port
,
370 struct net_device
*ndev
)
372 enum_netdev_ipv4_ips(ib_dev
, port
, ndev
);
373 if (IS_ENABLED(CONFIG_IPV6
))
374 enum_netdev_ipv6_ips(ib_dev
, port
, ndev
);
377 static void add_netdev_ips(struct ib_device
*ib_dev
, u8 port
,
378 struct net_device
*rdma_ndev
, void *cookie
)
380 enum_netdev_default_gids(ib_dev
, port
, cookie
, rdma_ndev
);
381 _add_netdev_ips(ib_dev
, port
, cookie
);
384 static void del_netdev_ips(struct ib_device
*ib_dev
, u8 port
,
385 struct net_device
*rdma_ndev
, void *cookie
)
387 ib_cache_gid_del_all_netdev_gids(ib_dev
, port
, cookie
);
390 static void enum_all_gids_of_dev_cb(struct ib_device
*ib_dev
,
392 struct net_device
*rdma_ndev
,
396 struct net_device
*ndev
;
398 /* Lock the rtnl to make sure the netdevs does not move under
403 for_each_netdev(net
, ndev
)
404 if (is_eth_port_of_netdev(ib_dev
, port
, rdma_ndev
, ndev
))
405 add_netdev_ips(ib_dev
, port
, rdma_ndev
, ndev
);
409 /* This function will rescan all of the network devices in the system
410 * and add their gids, as needed, to the relevant RoCE devices. */
411 int roce_rescan_device(struct ib_device
*ib_dev
)
413 ib_enum_roce_netdev(ib_dev
, pass_all_filter
, NULL
,
414 enum_all_gids_of_dev_cb
, NULL
);
419 static void callback_for_addr_gid_device_scan(struct ib_device
*device
,
421 struct net_device
*rdma_ndev
,
424 struct update_gid_event_work
*parsed
= cookie
;
426 return update_gid(parsed
->gid_op
, device
,
432 struct list_head list
;
433 struct net_device
*upper
;
436 static int netdev_upper_walk(struct net_device
*upper
, void *data
)
438 struct upper_list
*entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
439 struct list_head
*upper_list
= data
;
444 list_add_tail(&entry
->list
, upper_list
);
446 entry
->upper
= upper
;
451 static void handle_netdev_upper(struct ib_device
*ib_dev
, u8 port
,
453 void (*handle_netdev
)(struct ib_device
*ib_dev
,
455 struct net_device
*ndev
))
457 struct net_device
*ndev
= cookie
;
458 struct upper_list
*upper_iter
;
459 struct upper_list
*upper_temp
;
460 LIST_HEAD(upper_list
);
463 netdev_walk_all_upper_dev_rcu(ndev
, netdev_upper_walk
, &upper_list
);
466 handle_netdev(ib_dev
, port
, ndev
);
467 list_for_each_entry_safe(upper_iter
, upper_temp
, &upper_list
,
469 handle_netdev(ib_dev
, port
, upper_iter
->upper
);
470 dev_put(upper_iter
->upper
);
471 list_del(&upper_iter
->list
);
476 static void _roce_del_all_netdev_gids(struct ib_device
*ib_dev
, u8 port
,
477 struct net_device
*event_ndev
)
479 ib_cache_gid_del_all_netdev_gids(ib_dev
, port
, event_ndev
);
482 static void del_netdev_upper_ips(struct ib_device
*ib_dev
, u8 port
,
483 struct net_device
*rdma_ndev
, void *cookie
)
485 handle_netdev_upper(ib_dev
, port
, cookie
, _roce_del_all_netdev_gids
);
488 static void add_netdev_upper_ips(struct ib_device
*ib_dev
, u8 port
,
489 struct net_device
*rdma_ndev
, void *cookie
)
491 handle_netdev_upper(ib_dev
, port
, cookie
, _add_netdev_ips
);
494 static void del_netdev_default_ips_join(struct ib_device
*ib_dev
, u8 port
,
495 struct net_device
*rdma_ndev
,
498 struct net_device
*master_ndev
;
501 master_ndev
= netdev_master_upper_dev_get_rcu(rdma_ndev
);
503 dev_hold(master_ndev
);
507 bond_delete_netdev_default_gids(ib_dev
, port
, master_ndev
,
509 dev_put(master_ndev
);
513 static void del_netdev_default_ips(struct ib_device
*ib_dev
, u8 port
,
514 struct net_device
*rdma_ndev
, void *cookie
)
516 bond_delete_netdev_default_gids(ib_dev
, port
, cookie
, rdma_ndev
);
519 /* The following functions operate on all IB devices. netdevice_event and
520 * addr_event execute ib_enum_all_roce_netdevs through a work.
521 * ib_enum_all_roce_netdevs iterates through all IB devices.
524 static void netdevice_event_work_handler(struct work_struct
*_work
)
526 struct netdev_event_work
*work
=
527 container_of(_work
, struct netdev_event_work
, work
);
530 for (i
= 0; i
< ARRAY_SIZE(work
->cmds
) && work
->cmds
[i
].cb
; i
++) {
531 ib_enum_all_roce_netdevs(work
->cmds
[i
].filter
,
532 work
->cmds
[i
].filter_ndev
,
535 dev_put(work
->cmds
[i
].ndev
);
536 dev_put(work
->cmds
[i
].filter_ndev
);
542 static int netdevice_queue_work(struct netdev_event_work_cmd
*cmds
,
543 struct net_device
*ndev
)
546 struct netdev_event_work
*ndev_work
=
547 kmalloc(sizeof(*ndev_work
), GFP_KERNEL
);
552 memcpy(ndev_work
->cmds
, cmds
, sizeof(ndev_work
->cmds
));
553 for (i
= 0; i
< ARRAY_SIZE(ndev_work
->cmds
) && ndev_work
->cmds
[i
].cb
; i
++) {
554 if (!ndev_work
->cmds
[i
].ndev
)
555 ndev_work
->cmds
[i
].ndev
= ndev
;
556 if (!ndev_work
->cmds
[i
].filter_ndev
)
557 ndev_work
->cmds
[i
].filter_ndev
= ndev
;
558 dev_hold(ndev_work
->cmds
[i
].ndev
);
559 dev_hold(ndev_work
->cmds
[i
].filter_ndev
);
561 INIT_WORK(&ndev_work
->work
, netdevice_event_work_handler
);
563 queue_work(ib_wq
, &ndev_work
->work
);
568 static const struct netdev_event_work_cmd add_cmd
= {
569 .cb
= add_netdev_ips
, .filter
= is_eth_port_of_netdev
};
570 static const struct netdev_event_work_cmd add_cmd_upper_ips
= {
571 .cb
= add_netdev_upper_ips
, .filter
= is_eth_port_of_netdev
};
573 static void netdevice_event_changeupper(struct netdev_notifier_changeupper_info
*changeupper_info
,
574 struct netdev_event_work_cmd
*cmds
)
576 static const struct netdev_event_work_cmd upper_ips_del_cmd
= {
577 .cb
= del_netdev_upper_ips
, .filter
= upper_device_filter
};
578 static const struct netdev_event_work_cmd bonding_default_del_cmd
= {
579 .cb
= del_netdev_default_ips
, .filter
= is_eth_port_inactive_slave
};
581 if (changeupper_info
->linking
== false) {
582 cmds
[0] = upper_ips_del_cmd
;
583 cmds
[0].ndev
= changeupper_info
->upper_dev
;
586 cmds
[0] = bonding_default_del_cmd
;
587 cmds
[0].ndev
= changeupper_info
->upper_dev
;
588 cmds
[1] = add_cmd_upper_ips
;
589 cmds
[1].ndev
= changeupper_info
->upper_dev
;
590 cmds
[1].filter_ndev
= changeupper_info
->upper_dev
;
594 static int netdevice_event(struct notifier_block
*this, unsigned long event
,
597 static const struct netdev_event_work_cmd del_cmd
= {
598 .cb
= del_netdev_ips
, .filter
= pass_all_filter
};
599 static const struct netdev_event_work_cmd bonding_default_del_cmd_join
= {
600 .cb
= del_netdev_default_ips_join
, .filter
= is_eth_port_inactive_slave
};
601 static const struct netdev_event_work_cmd default_del_cmd
= {
602 .cb
= del_netdev_default_ips
, .filter
= pass_all_filter
};
603 static const struct netdev_event_work_cmd bonding_event_ips_del_cmd
= {
604 .cb
= del_netdev_upper_ips
, .filter
= upper_device_filter
};
605 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
606 struct netdev_event_work_cmd cmds
[ROCE_NETDEV_CALLBACK_SZ
] = { {NULL
} };
608 if (ndev
->type
!= ARPHRD_ETHER
)
612 case NETDEV_REGISTER
:
614 cmds
[0] = bonding_default_del_cmd_join
;
618 case NETDEV_UNREGISTER
:
619 if (ndev
->reg_state
< NETREG_UNREGISTERED
)
625 case NETDEV_CHANGEADDR
:
626 cmds
[0] = default_del_cmd
;
630 case NETDEV_CHANGEUPPER
:
631 netdevice_event_changeupper(
632 container_of(ptr
, struct netdev_notifier_changeupper_info
, info
),
636 case NETDEV_BONDING_FAILOVER
:
637 cmds
[0] = bonding_event_ips_del_cmd
;
638 cmds
[1] = bonding_default_del_cmd_join
;
639 cmds
[2] = add_cmd_upper_ips
;
646 return netdevice_queue_work(cmds
, ndev
);
649 static void update_gid_event_work_handler(struct work_struct
*_work
)
651 struct update_gid_event_work
*work
=
652 container_of(_work
, struct update_gid_event_work
, work
);
654 ib_enum_all_roce_netdevs(is_eth_port_of_netdev
, work
->gid_attr
.ndev
,
655 callback_for_addr_gid_device_scan
, work
);
657 dev_put(work
->gid_attr
.ndev
);
661 static int addr_event(struct notifier_block
*this, unsigned long event
,
662 struct sockaddr
*sa
, struct net_device
*ndev
)
664 struct update_gid_event_work
*work
;
665 enum gid_op_type gid_op
;
667 if (ndev
->type
!= ARPHRD_ETHER
)
683 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
687 INIT_WORK(&work
->work
, update_gid_event_work_handler
);
689 rdma_ip2gid(sa
, &work
->gid
);
690 work
->gid_op
= gid_op
;
692 memset(&work
->gid_attr
, 0, sizeof(work
->gid_attr
));
694 work
->gid_attr
.ndev
= ndev
;
696 queue_work(ib_wq
, &work
->work
);
701 static int inetaddr_event(struct notifier_block
*this, unsigned long event
,
704 struct sockaddr_in in
;
705 struct net_device
*ndev
;
706 struct in_ifaddr
*ifa
= ptr
;
708 in
.sin_family
= AF_INET
;
709 in
.sin_addr
.s_addr
= ifa
->ifa_address
;
710 ndev
= ifa
->ifa_dev
->dev
;
712 return addr_event(this, event
, (struct sockaddr
*)&in
, ndev
);
715 static int inet6addr_event(struct notifier_block
*this, unsigned long event
,
718 struct sockaddr_in6 in6
;
719 struct net_device
*ndev
;
720 struct inet6_ifaddr
*ifa6
= ptr
;
722 in6
.sin6_family
= AF_INET6
;
723 in6
.sin6_addr
= ifa6
->addr
;
724 ndev
= ifa6
->idev
->dev
;
726 return addr_event(this, event
, (struct sockaddr
*)&in6
, ndev
);
729 static struct notifier_block nb_netdevice
= {
730 .notifier_call
= netdevice_event
733 static struct notifier_block nb_inetaddr
= {
734 .notifier_call
= inetaddr_event
737 static struct notifier_block nb_inet6addr
= {
738 .notifier_call
= inet6addr_event
741 int __init
roce_gid_mgmt_init(void)
743 register_inetaddr_notifier(&nb_inetaddr
);
744 if (IS_ENABLED(CONFIG_IPV6
))
745 register_inet6addr_notifier(&nb_inet6addr
);
746 /* We relay on the netdevice notifier to enumerate all
747 * existing devices in the system. Register to this notifier
748 * last to make sure we will not miss any IP add/del
751 register_netdevice_notifier(&nb_netdevice
);
756 void __exit
roce_gid_mgmt_cleanup(void)
758 if (IS_ENABLED(CONFIG_IPV6
))
759 unregister_inet6addr_notifier(&nb_inet6addr
);
760 unregister_inetaddr_notifier(&nb_inetaddr
);
761 unregister_netdevice_notifier(&nb_netdevice
);
762 /* Ensure all gid deletion tasks complete before we go down,
763 * to avoid any reference to free'd memory. By the time
764 * ib-core is removed, all physical devices have been removed,
765 * so no issue with remaining hardware contexts.