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>
41 #include <rdma/ib_cache.h>
42 #include <rdma/ib_addr.h>
49 struct update_gid_event_work {
50 struct work_struct work;
52 struct ib_gid_attr gid_attr;
53 enum gid_op_type gid_op;
56 #define ROCE_NETDEV_CALLBACK_SZ 2
57 struct netdev_event_work_cmd {
58 roce_netdev_callback cb;
59 roce_netdev_filter filter;
62 struct netdev_event_work {
63 struct work_struct work;
64 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ];
65 struct net_device *ndev;
68 static void update_gid(enum gid_op_type gid_op, struct ib_device *ib_dev,
69 u8 port, union ib_gid *gid,
70 struct ib_gid_attr *gid_attr)
74 ib_cache_gid_add(ib_dev, port, gid, gid_attr);
77 ib_cache_gid_del(ib_dev, port, gid, gid_attr);
82 static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
83 struct net_device *rdma_ndev, void *cookie)
85 struct net_device *real_dev;
86 struct net_device *master_dev;
87 struct net_device *event_ndev = (struct net_device *)cookie;
94 master_dev = netdev_master_upper_dev_get_rcu(rdma_ndev);
95 real_dev = rdma_vlan_dev_real_dev(event_ndev);
96 res = (real_dev ? real_dev : event_ndev) ==
97 (master_dev ? master_dev : rdma_ndev);
103 static int pass_all_filter(struct ib_device *ib_dev, u8 port,
104 struct net_device *rdma_ndev, void *cookie)
109 static void update_gid_ip(enum gid_op_type gid_op,
110 struct ib_device *ib_dev,
111 u8 port, struct net_device *ndev,
112 struct sockaddr *addr)
115 struct ib_gid_attr gid_attr;
117 rdma_ip2gid(addr, &gid);
118 memset(&gid_attr, 0, sizeof(gid_attr));
119 gid_attr.ndev = ndev;
121 update_gid(gid_op, ib_dev, port, &gid, &gid_attr);
124 static void enum_netdev_default_gids(struct ib_device *ib_dev,
125 u8 port, struct net_device *event_ndev,
126 struct net_device *rdma_ndev)
128 if (rdma_ndev != event_ndev)
131 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
132 IB_CACHE_GID_DEFAULT_MODE_SET);
135 static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
136 u8 port, struct net_device *ndev)
138 struct in_device *in_dev;
140 if (ndev->reg_state >= NETREG_UNREGISTERING)
143 in_dev = in_dev_get(ndev);
148 struct sockaddr_in ip;
150 ip.sin_family = AF_INET;
151 ip.sin_addr.s_addr = ifa->ifa_address;
152 update_gid_ip(GID_ADD, ib_dev, port, ndev,
153 (struct sockaddr *)&ip);
160 static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
161 u8 port, struct net_device *ndev)
163 struct inet6_ifaddr *ifp;
164 struct inet6_dev *in6_dev;
166 struct list_head list;
167 struct sockaddr_in6 sin6;
169 struct sin6_list *sin6_iter;
170 struct sin6_list *sin6_temp;
171 struct ib_gid_attr gid_attr = {.ndev = ndev};
172 LIST_HEAD(sin6_list);
174 if (ndev->reg_state >= NETREG_UNREGISTERING)
177 in6_dev = in6_dev_get(ndev);
181 read_lock_bh(&in6_dev->lock);
182 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
183 struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
186 pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv6 update\n");
190 entry->sin6.sin6_family = AF_INET6;
191 entry->sin6.sin6_addr = ifp->addr;
192 list_add_tail(&entry->list, &sin6_list);
194 read_unlock_bh(&in6_dev->lock);
196 in6_dev_put(in6_dev);
198 list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
201 rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
202 update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
203 list_del(&sin6_iter->list);
208 static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
209 struct net_device *rdma_ndev, void *cookie)
211 struct net_device *event_ndev = (struct net_device *)cookie;
213 enum_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
214 enum_netdev_ipv4_ips(ib_dev, port, event_ndev);
215 if (IS_ENABLED(CONFIG_IPV6))
216 enum_netdev_ipv6_ips(ib_dev, port, event_ndev);
219 static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
220 struct net_device *rdma_ndev, void *cookie)
222 struct net_device *event_ndev = (struct net_device *)cookie;
224 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
227 static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
229 struct net_device *rdma_ndev,
233 struct net_device *ndev;
235 /* Lock the rtnl to make sure the netdevs does not move under
240 for_each_netdev(net, ndev)
241 if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
242 add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
246 /* This function will rescan all of the network devices in the system
247 * and add their gids, as needed, to the relevant RoCE devices. */
248 int roce_rescan_device(struct ib_device *ib_dev)
250 ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
251 enum_all_gids_of_dev_cb, NULL);
256 static void callback_for_addr_gid_device_scan(struct ib_device *device,
258 struct net_device *rdma_ndev,
261 struct update_gid_event_work *parsed = cookie;
263 return update_gid(parsed->gid_op, device,
268 /* The following functions operate on all IB devices. netdevice_event and
269 * addr_event execute ib_enum_all_roce_netdevs through a work.
270 * ib_enum_all_roce_netdevs iterates through all IB devices.
273 static void netdevice_event_work_handler(struct work_struct *_work)
275 struct netdev_event_work *work =
276 container_of(_work, struct netdev_event_work, work);
279 for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++)
280 ib_enum_all_roce_netdevs(work->cmds[i].filter, work->ndev,
281 work->cmds[i].cb, work->ndev);
287 static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
288 struct net_device *ndev)
290 struct netdev_event_work *ndev_work =
291 kmalloc(sizeof(*ndev_work), GFP_KERNEL);
294 pr_warn("roce_gid_mgmt: can't allocate work for netdevice_event\n");
298 memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
299 ndev_work->ndev = ndev;
301 INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
303 queue_work(ib_wq, &ndev_work->work);
308 static int netdevice_event(struct notifier_block *this, unsigned long event,
311 static const struct netdev_event_work_cmd add_cmd = {
312 .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
313 static const struct netdev_event_work_cmd del_cmd = {
314 .cb = del_netdev_ips, .filter = pass_all_filter};
315 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
316 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
318 if (ndev->type != ARPHRD_ETHER)
322 case NETDEV_REGISTER:
327 case NETDEV_UNREGISTER:
328 if (ndev->reg_state < NETREG_UNREGISTERED)
334 case NETDEV_CHANGEADDR:
342 return netdevice_queue_work(cmds, ndev);
345 static void update_gid_event_work_handler(struct work_struct *_work)
347 struct update_gid_event_work *work =
348 container_of(_work, struct update_gid_event_work, work);
350 ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
351 callback_for_addr_gid_device_scan, work);
353 dev_put(work->gid_attr.ndev);
357 static int addr_event(struct notifier_block *this, unsigned long event,
358 struct sockaddr *sa, struct net_device *ndev)
360 struct update_gid_event_work *work;
361 enum gid_op_type gid_op;
363 if (ndev->type != ARPHRD_ETHER)
379 work = kmalloc(sizeof(*work), GFP_ATOMIC);
381 pr_warn("roce_gid_mgmt: Couldn't allocate work for addr_event\n");
385 INIT_WORK(&work->work, update_gid_event_work_handler);
387 rdma_ip2gid(sa, &work->gid);
388 work->gid_op = gid_op;
390 memset(&work->gid_attr, 0, sizeof(work->gid_attr));
392 work->gid_attr.ndev = ndev;
394 queue_work(ib_wq, &work->work);
399 static int inetaddr_event(struct notifier_block *this, unsigned long event,
402 struct sockaddr_in in;
403 struct net_device *ndev;
404 struct in_ifaddr *ifa = ptr;
406 in.sin_family = AF_INET;
407 in.sin_addr.s_addr = ifa->ifa_address;
408 ndev = ifa->ifa_dev->dev;
410 return addr_event(this, event, (struct sockaddr *)&in, ndev);
413 static int inet6addr_event(struct notifier_block *this, unsigned long event,
416 struct sockaddr_in6 in6;
417 struct net_device *ndev;
418 struct inet6_ifaddr *ifa6 = ptr;
420 in6.sin6_family = AF_INET6;
421 in6.sin6_addr = ifa6->addr;
422 ndev = ifa6->idev->dev;
424 return addr_event(this, event, (struct sockaddr *)&in6, ndev);
427 static struct notifier_block nb_netdevice = {
428 .notifier_call = netdevice_event
431 static struct notifier_block nb_inetaddr = {
432 .notifier_call = inetaddr_event
435 static struct notifier_block nb_inet6addr = {
436 .notifier_call = inet6addr_event
439 int __init roce_gid_mgmt_init(void)
441 register_inetaddr_notifier(&nb_inetaddr);
442 if (IS_ENABLED(CONFIG_IPV6))
443 register_inet6addr_notifier(&nb_inet6addr);
444 /* We relay on the netdevice notifier to enumerate all
445 * existing devices in the system. Register to this notifier
446 * last to make sure we will not miss any IP add/del
449 register_netdevice_notifier(&nb_netdevice);
454 void __exit roce_gid_mgmt_cleanup(void)
456 if (IS_ENABLED(CONFIG_IPV6))
457 unregister_inet6addr_notifier(&nb_inet6addr);
458 unregister_inetaddr_notifier(&nb_inetaddr);
459 unregister_netdevice_notifier(&nb_netdevice);
460 /* Ensure all gid deletion tasks complete before we go down,
461 * to avoid any reference to free'd memory. By the time
462 * ib-core is removed, all physical devices have been removed,
463 * so no issue with remaining hardware contexts.