roce_gid_mgmt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Copyright (c) 2015, Mellanox Technologies inc. All rights reserved.
  3. *
  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:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  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.
  22. *
  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
  30. * SOFTWARE.
  31. */
  32. #include "core_priv.h"
  33. #include <linux/in.h>
  34. #include <linux/in6.h>
  35. /* For in6_dev_get/in6_dev_put */
  36. #include <net/addrconf.h>
  37. #include <net/bonding.h>
  38. #include <rdma/ib_cache.h>
  39. #include <rdma/ib_addr.h>
  40. enum gid_op_type {
  41. GID_DEL = 0,
  42. GID_ADD
  43. };
  44. struct update_gid_event_work {
  45. struct work_struct work;
  46. union ib_gid gid;
  47. struct ib_gid_attr gid_attr;
  48. enum gid_op_type gid_op;
  49. };
  50. #define ROCE_NETDEV_CALLBACK_SZ 3
  51. struct netdev_event_work_cmd {
  52. roce_netdev_callback cb;
  53. roce_netdev_filter filter;
  54. struct net_device *ndev;
  55. struct net_device *filter_ndev;
  56. };
  57. struct netdev_event_work {
  58. struct work_struct work;
  59. struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ];
  60. };
  61. static const struct {
  62. bool (*is_supported)(const struct ib_device *device, u8 port_num);
  63. enum ib_gid_type gid_type;
  64. } PORT_CAP_TO_GID_TYPE[] = {
  65. {rdma_protocol_roce_eth_encap, IB_GID_TYPE_ROCE},
  66. {rdma_protocol_roce_udp_encap, IB_GID_TYPE_ROCE_UDP_ENCAP},
  67. };
  68. #define CAP_TO_GID_TABLE_SIZE ARRAY_SIZE(PORT_CAP_TO_GID_TYPE)
  69. unsigned long roce_gid_type_mask_support(struct ib_device *ib_dev, u8 port)
  70. {
  71. int i;
  72. unsigned int ret_flags = 0;
  73. if (!rdma_protocol_roce(ib_dev, port))
  74. return 1UL << IB_GID_TYPE_IB;
  75. for (i = 0; i < CAP_TO_GID_TABLE_SIZE; i++)
  76. if (PORT_CAP_TO_GID_TYPE[i].is_supported(ib_dev, port))
  77. ret_flags |= 1UL << PORT_CAP_TO_GID_TYPE[i].gid_type;
  78. return ret_flags;
  79. }
  80. EXPORT_SYMBOL(roce_gid_type_mask_support);
  81. static void update_gid(enum gid_op_type gid_op, struct ib_device *ib_dev,
  82. u8 port, union ib_gid *gid,
  83. struct ib_gid_attr *gid_attr)
  84. {
  85. int i;
  86. unsigned long gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
  87. for (i = 0; i < IB_GID_TYPE_SIZE; i++) {
  88. if ((1UL << i) & gid_type_mask) {
  89. gid_attr->gid_type = i;
  90. switch (gid_op) {
  91. case GID_ADD:
  92. ib_cache_gid_add(ib_dev, port,
  93. gid, gid_attr);
  94. break;
  95. case GID_DEL:
  96. ib_cache_gid_del(ib_dev, port,
  97. gid, gid_attr);
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. enum bonding_slave_state {
  104. BONDING_SLAVE_STATE_ACTIVE = 1UL << 0,
  105. BONDING_SLAVE_STATE_INACTIVE = 1UL << 1,
  106. /* No primary slave or the device isn't a slave in bonding */
  107. BONDING_SLAVE_STATE_NA = 1UL << 2,
  108. };
  109. static enum bonding_slave_state is_eth_active_slave_of_bonding_rcu(struct net_device *dev,
  110. struct net_device *upper)
  111. {
  112. if (upper && netif_is_bond_master(upper)) {
  113. struct net_device *pdev =
  114. bond_option_active_slave_get_rcu(netdev_priv(upper));
  115. if (pdev)
  116. return dev == pdev ? BONDING_SLAVE_STATE_ACTIVE :
  117. BONDING_SLAVE_STATE_INACTIVE;
  118. }
  119. return BONDING_SLAVE_STATE_NA;
  120. }
  121. #define REQUIRED_BOND_STATES (BONDING_SLAVE_STATE_ACTIVE | \
  122. BONDING_SLAVE_STATE_NA)
  123. static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
  124. struct net_device *rdma_ndev, void *cookie)
  125. {
  126. struct net_device *real_dev;
  127. int res;
  128. if (!rdma_ndev)
  129. return 0;
  130. rcu_read_lock();
  131. real_dev = rdma_vlan_dev_real_dev(cookie);
  132. if (!real_dev)
  133. real_dev = cookie;
  134. res = ((rdma_is_upper_dev_rcu(rdma_ndev, cookie) &&
  135. (is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) &
  136. REQUIRED_BOND_STATES)) ||
  137. real_dev == rdma_ndev);
  138. rcu_read_unlock();
  139. return res;
  140. }
  141. static int is_eth_port_inactive_slave(struct ib_device *ib_dev, u8 port,
  142. struct net_device *rdma_ndev, void *cookie)
  143. {
  144. struct net_device *master_dev;
  145. int res;
  146. if (!rdma_ndev)
  147. return 0;
  148. rcu_read_lock();
  149. master_dev = netdev_master_upper_dev_get_rcu(rdma_ndev);
  150. res = is_eth_active_slave_of_bonding_rcu(rdma_ndev, master_dev) ==
  151. BONDING_SLAVE_STATE_INACTIVE;
  152. rcu_read_unlock();
  153. return res;
  154. }
  155. static int pass_all_filter(struct ib_device *ib_dev, u8 port,
  156. struct net_device *rdma_ndev, void *cookie)
  157. {
  158. return 1;
  159. }
  160. static int upper_device_filter(struct ib_device *ib_dev, u8 port,
  161. struct net_device *rdma_ndev, void *cookie)
  162. {
  163. int res;
  164. if (!rdma_ndev)
  165. return 0;
  166. if (rdma_ndev == cookie)
  167. return 1;
  168. rcu_read_lock();
  169. res = rdma_is_upper_dev_rcu(rdma_ndev, cookie);
  170. rcu_read_unlock();
  171. return res;
  172. }
  173. static void update_gid_ip(enum gid_op_type gid_op,
  174. struct ib_device *ib_dev,
  175. u8 port, struct net_device *ndev,
  176. struct sockaddr *addr)
  177. {
  178. union ib_gid gid;
  179. struct ib_gid_attr gid_attr;
  180. rdma_ip2gid(addr, &gid);
  181. memset(&gid_attr, 0, sizeof(gid_attr));
  182. gid_attr.ndev = ndev;
  183. update_gid(gid_op, ib_dev, port, &gid, &gid_attr);
  184. }
  185. static void enum_netdev_default_gids(struct ib_device *ib_dev,
  186. u8 port, struct net_device *event_ndev,
  187. struct net_device *rdma_ndev)
  188. {
  189. unsigned long gid_type_mask;
  190. rcu_read_lock();
  191. if (!rdma_ndev ||
  192. ((rdma_ndev != event_ndev &&
  193. !rdma_is_upper_dev_rcu(rdma_ndev, event_ndev)) ||
  194. is_eth_active_slave_of_bonding_rcu(rdma_ndev,
  195. netdev_master_upper_dev_get_rcu(rdma_ndev)) ==
  196. BONDING_SLAVE_STATE_INACTIVE)) {
  197. rcu_read_unlock();
  198. return;
  199. }
  200. rcu_read_unlock();
  201. gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
  202. ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev, gid_type_mask,
  203. IB_CACHE_GID_DEFAULT_MODE_SET);
  204. }
  205. static void bond_delete_netdev_default_gids(struct ib_device *ib_dev,
  206. u8 port,
  207. struct net_device *event_ndev,
  208. struct net_device *rdma_ndev)
  209. {
  210. struct net_device *real_dev = rdma_vlan_dev_real_dev(event_ndev);
  211. if (!rdma_ndev)
  212. return;
  213. if (!real_dev)
  214. real_dev = event_ndev;
  215. rcu_read_lock();
  216. if (rdma_is_upper_dev_rcu(rdma_ndev, event_ndev) &&
  217. is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) ==
  218. BONDING_SLAVE_STATE_INACTIVE) {
  219. unsigned long gid_type_mask;
  220. rcu_read_unlock();
  221. gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
  222. ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
  223. gid_type_mask,
  224. IB_CACHE_GID_DEFAULT_MODE_DELETE);
  225. } else {
  226. rcu_read_unlock();
  227. }
  228. }
  229. static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
  230. u8 port, struct net_device *ndev)
  231. {
  232. struct in_device *in_dev;
  233. struct sin_list {
  234. struct list_head list;
  235. struct sockaddr_in ip;
  236. };
  237. struct sin_list *sin_iter;
  238. struct sin_list *sin_temp;
  239. LIST_HEAD(sin_list);
  240. if (ndev->reg_state >= NETREG_UNREGISTERING)
  241. return;
  242. rcu_read_lock();
  243. in_dev = __in_dev_get_rcu(ndev);
  244. if (!in_dev) {
  245. rcu_read_unlock();
  246. return;
  247. }
  248. for_ifa(in_dev) {
  249. struct sin_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  250. if (!entry)
  251. continue;
  252. entry->ip.sin_family = AF_INET;
  253. entry->ip.sin_addr.s_addr = ifa->ifa_address;
  254. list_add_tail(&entry->list, &sin_list);
  255. }
  256. endfor_ifa(in_dev);
  257. rcu_read_unlock();
  258. list_for_each_entry_safe(sin_iter, sin_temp, &sin_list, list) {
  259. update_gid_ip(GID_ADD, ib_dev, port, ndev,
  260. (struct sockaddr *)&sin_iter->ip);
  261. list_del(&sin_iter->list);
  262. kfree(sin_iter);
  263. }
  264. }
  265. static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
  266. u8 port, struct net_device *ndev)
  267. {
  268. struct inet6_ifaddr *ifp;
  269. struct inet6_dev *in6_dev;
  270. struct sin6_list {
  271. struct list_head list;
  272. struct sockaddr_in6 sin6;
  273. };
  274. struct sin6_list *sin6_iter;
  275. struct sin6_list *sin6_temp;
  276. struct ib_gid_attr gid_attr = {.ndev = ndev};
  277. LIST_HEAD(sin6_list);
  278. if (ndev->reg_state >= NETREG_UNREGISTERING)
  279. return;
  280. in6_dev = in6_dev_get(ndev);
  281. if (!in6_dev)
  282. return;
  283. read_lock_bh(&in6_dev->lock);
  284. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  285. struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  286. if (!entry)
  287. continue;
  288. entry->sin6.sin6_family = AF_INET6;
  289. entry->sin6.sin6_addr = ifp->addr;
  290. list_add_tail(&entry->list, &sin6_list);
  291. }
  292. read_unlock_bh(&in6_dev->lock);
  293. in6_dev_put(in6_dev);
  294. list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
  295. union ib_gid gid;
  296. rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
  297. update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
  298. list_del(&sin6_iter->list);
  299. kfree(sin6_iter);
  300. }
  301. }
  302. static void _add_netdev_ips(struct ib_device *ib_dev, u8 port,
  303. struct net_device *ndev)
  304. {
  305. enum_netdev_ipv4_ips(ib_dev, port, ndev);
  306. if (IS_ENABLED(CONFIG_IPV6))
  307. enum_netdev_ipv6_ips(ib_dev, port, ndev);
  308. }
  309. static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
  310. struct net_device *rdma_ndev, void *cookie)
  311. {
  312. enum_netdev_default_gids(ib_dev, port, cookie, rdma_ndev);
  313. _add_netdev_ips(ib_dev, port, cookie);
  314. }
  315. static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
  316. struct net_device *rdma_ndev, void *cookie)
  317. {
  318. ib_cache_gid_del_all_netdev_gids(ib_dev, port, cookie);
  319. }
  320. static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
  321. u8 port,
  322. struct net_device *rdma_ndev,
  323. void *cookie)
  324. {
  325. struct net *net;
  326. struct net_device *ndev;
  327. /* Lock the rtnl to make sure the netdevs does not move under
  328. * our feet
  329. */
  330. rtnl_lock();
  331. for_each_net(net)
  332. for_each_netdev(net, ndev)
  333. if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
  334. add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
  335. rtnl_unlock();
  336. }
  337. /* This function will rescan all of the network devices in the system
  338. * and add their gids, as needed, to the relevant RoCE devices. */
  339. int roce_rescan_device(struct ib_device *ib_dev)
  340. {
  341. ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
  342. enum_all_gids_of_dev_cb, NULL);
  343. return 0;
  344. }
  345. static void callback_for_addr_gid_device_scan(struct ib_device *device,
  346. u8 port,
  347. struct net_device *rdma_ndev,
  348. void *cookie)
  349. {
  350. struct update_gid_event_work *parsed = cookie;
  351. return update_gid(parsed->gid_op, device,
  352. port, &parsed->gid,
  353. &parsed->gid_attr);
  354. }
  355. struct upper_list {
  356. struct list_head list;
  357. struct net_device *upper;
  358. };
  359. static int netdev_upper_walk(struct net_device *upper, void *data)
  360. {
  361. struct upper_list *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  362. struct list_head *upper_list = data;
  363. if (!entry)
  364. return 0;
  365. list_add_tail(&entry->list, upper_list);
  366. dev_hold(upper);
  367. entry->upper = upper;
  368. return 0;
  369. }
  370. static void handle_netdev_upper(struct ib_device *ib_dev, u8 port,
  371. void *cookie,
  372. void (*handle_netdev)(struct ib_device *ib_dev,
  373. u8 port,
  374. struct net_device *ndev))
  375. {
  376. struct net_device *ndev = cookie;
  377. struct upper_list *upper_iter;
  378. struct upper_list *upper_temp;
  379. LIST_HEAD(upper_list);
  380. rcu_read_lock();
  381. netdev_walk_all_upper_dev_rcu(ndev, netdev_upper_walk, &upper_list);
  382. rcu_read_unlock();
  383. handle_netdev(ib_dev, port, ndev);
  384. list_for_each_entry_safe(upper_iter, upper_temp, &upper_list,
  385. list) {
  386. handle_netdev(ib_dev, port, upper_iter->upper);
  387. dev_put(upper_iter->upper);
  388. list_del(&upper_iter->list);
  389. kfree(upper_iter);
  390. }
  391. }
  392. static void _roce_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
  393. struct net_device *event_ndev)
  394. {
  395. ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
  396. }
  397. static void del_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
  398. struct net_device *rdma_ndev, void *cookie)
  399. {
  400. handle_netdev_upper(ib_dev, port, cookie, _roce_del_all_netdev_gids);
  401. }
  402. static void add_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
  403. struct net_device *rdma_ndev, void *cookie)
  404. {
  405. handle_netdev_upper(ib_dev, port, cookie, _add_netdev_ips);
  406. }
  407. static void del_netdev_default_ips_join(struct ib_device *ib_dev, u8 port,
  408. struct net_device *rdma_ndev,
  409. void *cookie)
  410. {
  411. struct net_device *master_ndev;
  412. rcu_read_lock();
  413. master_ndev = netdev_master_upper_dev_get_rcu(rdma_ndev);
  414. if (master_ndev)
  415. dev_hold(master_ndev);
  416. rcu_read_unlock();
  417. if (master_ndev) {
  418. bond_delete_netdev_default_gids(ib_dev, port, master_ndev,
  419. rdma_ndev);
  420. dev_put(master_ndev);
  421. }
  422. }
  423. static void del_netdev_default_ips(struct ib_device *ib_dev, u8 port,
  424. struct net_device *rdma_ndev, void *cookie)
  425. {
  426. bond_delete_netdev_default_gids(ib_dev, port, cookie, rdma_ndev);
  427. }
  428. /* The following functions operate on all IB devices. netdevice_event and
  429. * addr_event execute ib_enum_all_roce_netdevs through a work.
  430. * ib_enum_all_roce_netdevs iterates through all IB devices.
  431. */
  432. static void netdevice_event_work_handler(struct work_struct *_work)
  433. {
  434. struct netdev_event_work *work =
  435. container_of(_work, struct netdev_event_work, work);
  436. unsigned int i;
  437. for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++) {
  438. ib_enum_all_roce_netdevs(work->cmds[i].filter,
  439. work->cmds[i].filter_ndev,
  440. work->cmds[i].cb,
  441. work->cmds[i].ndev);
  442. dev_put(work->cmds[i].ndev);
  443. dev_put(work->cmds[i].filter_ndev);
  444. }
  445. kfree(work);
  446. }
  447. static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
  448. struct net_device *ndev)
  449. {
  450. unsigned int i;
  451. struct netdev_event_work *ndev_work =
  452. kmalloc(sizeof(*ndev_work), GFP_KERNEL);
  453. if (!ndev_work)
  454. return NOTIFY_DONE;
  455. memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
  456. for (i = 0; i < ARRAY_SIZE(ndev_work->cmds) && ndev_work->cmds[i].cb; i++) {
  457. if (!ndev_work->cmds[i].ndev)
  458. ndev_work->cmds[i].ndev = ndev;
  459. if (!ndev_work->cmds[i].filter_ndev)
  460. ndev_work->cmds[i].filter_ndev = ndev;
  461. dev_hold(ndev_work->cmds[i].ndev);
  462. dev_hold(ndev_work->cmds[i].filter_ndev);
  463. }
  464. INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
  465. queue_work(ib_wq, &ndev_work->work);
  466. return NOTIFY_DONE;
  467. }
  468. static const struct netdev_event_work_cmd add_cmd = {
  469. .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
  470. static const struct netdev_event_work_cmd add_cmd_upper_ips = {
  471. .cb = add_netdev_upper_ips, .filter = is_eth_port_of_netdev};
  472. static void netdevice_event_changeupper(struct netdev_notifier_changeupper_info *changeupper_info,
  473. struct netdev_event_work_cmd *cmds)
  474. {
  475. static const struct netdev_event_work_cmd upper_ips_del_cmd = {
  476. .cb = del_netdev_upper_ips, .filter = upper_device_filter};
  477. static const struct netdev_event_work_cmd bonding_default_del_cmd = {
  478. .cb = del_netdev_default_ips, .filter = is_eth_port_inactive_slave};
  479. if (changeupper_info->linking == false) {
  480. cmds[0] = upper_ips_del_cmd;
  481. cmds[0].ndev = changeupper_info->upper_dev;
  482. cmds[1] = add_cmd;
  483. } else {
  484. cmds[0] = bonding_default_del_cmd;
  485. cmds[0].ndev = changeupper_info->upper_dev;
  486. cmds[1] = add_cmd_upper_ips;
  487. cmds[1].ndev = changeupper_info->upper_dev;
  488. cmds[1].filter_ndev = changeupper_info->upper_dev;
  489. }
  490. }
  491. static int netdevice_event(struct notifier_block *this, unsigned long event,
  492. void *ptr)
  493. {
  494. static const struct netdev_event_work_cmd del_cmd = {
  495. .cb = del_netdev_ips, .filter = pass_all_filter};
  496. static const struct netdev_event_work_cmd bonding_default_del_cmd_join = {
  497. .cb = del_netdev_default_ips_join, .filter = is_eth_port_inactive_slave};
  498. static const struct netdev_event_work_cmd default_del_cmd = {
  499. .cb = del_netdev_default_ips, .filter = pass_all_filter};
  500. static const struct netdev_event_work_cmd bonding_event_ips_del_cmd = {
  501. .cb = del_netdev_upper_ips, .filter = upper_device_filter};
  502. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  503. struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
  504. if (ndev->type != ARPHRD_ETHER)
  505. return NOTIFY_DONE;
  506. switch (event) {
  507. case NETDEV_REGISTER:
  508. case NETDEV_UP:
  509. cmds[0] = bonding_default_del_cmd_join;
  510. cmds[1] = add_cmd;
  511. break;
  512. case NETDEV_UNREGISTER:
  513. if (ndev->reg_state < NETREG_UNREGISTERED)
  514. cmds[0] = del_cmd;
  515. else
  516. return NOTIFY_DONE;
  517. break;
  518. case NETDEV_CHANGEADDR:
  519. cmds[0] = default_del_cmd;
  520. cmds[1] = add_cmd;
  521. break;
  522. case NETDEV_CHANGEUPPER:
  523. netdevice_event_changeupper(
  524. container_of(ptr, struct netdev_notifier_changeupper_info, info),
  525. cmds);
  526. break;
  527. case NETDEV_BONDING_FAILOVER:
  528. cmds[0] = bonding_event_ips_del_cmd;
  529. cmds[1] = bonding_default_del_cmd_join;
  530. cmds[2] = add_cmd_upper_ips;
  531. break;
  532. default:
  533. return NOTIFY_DONE;
  534. }
  535. return netdevice_queue_work(cmds, ndev);
  536. }
  537. static void update_gid_event_work_handler(struct work_struct *_work)
  538. {
  539. struct update_gid_event_work *work =
  540. container_of(_work, struct update_gid_event_work, work);
  541. ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
  542. callback_for_addr_gid_device_scan, work);
  543. dev_put(work->gid_attr.ndev);
  544. kfree(work);
  545. }
  546. static int addr_event(struct notifier_block *this, unsigned long event,
  547. struct sockaddr *sa, struct net_device *ndev)
  548. {
  549. struct update_gid_event_work *work;
  550. enum gid_op_type gid_op;
  551. if (ndev->type != ARPHRD_ETHER)
  552. return NOTIFY_DONE;
  553. switch (event) {
  554. case NETDEV_UP:
  555. gid_op = GID_ADD;
  556. break;
  557. case NETDEV_DOWN:
  558. gid_op = GID_DEL;
  559. break;
  560. default:
  561. return NOTIFY_DONE;
  562. }
  563. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  564. if (!work)
  565. return NOTIFY_DONE;
  566. INIT_WORK(&work->work, update_gid_event_work_handler);
  567. rdma_ip2gid(sa, &work->gid);
  568. work->gid_op = gid_op;
  569. memset(&work->gid_attr, 0, sizeof(work->gid_attr));
  570. dev_hold(ndev);
  571. work->gid_attr.ndev = ndev;
  572. queue_work(ib_wq, &work->work);
  573. return NOTIFY_DONE;
  574. }
  575. static int inetaddr_event(struct notifier_block *this, unsigned long event,
  576. void *ptr)
  577. {
  578. struct sockaddr_in in;
  579. struct net_device *ndev;
  580. struct in_ifaddr *ifa = ptr;
  581. in.sin_family = AF_INET;
  582. in.sin_addr.s_addr = ifa->ifa_address;
  583. ndev = ifa->ifa_dev->dev;
  584. return addr_event(this, event, (struct sockaddr *)&in, ndev);
  585. }
  586. static int inet6addr_event(struct notifier_block *this, unsigned long event,
  587. void *ptr)
  588. {
  589. struct sockaddr_in6 in6;
  590. struct net_device *ndev;
  591. struct inet6_ifaddr *ifa6 = ptr;
  592. in6.sin6_family = AF_INET6;
  593. in6.sin6_addr = ifa6->addr;
  594. ndev = ifa6->idev->dev;
  595. return addr_event(this, event, (struct sockaddr *)&in6, ndev);
  596. }
  597. static struct notifier_block nb_netdevice = {
  598. .notifier_call = netdevice_event
  599. };
  600. static struct notifier_block nb_inetaddr = {
  601. .notifier_call = inetaddr_event
  602. };
  603. static struct notifier_block nb_inet6addr = {
  604. .notifier_call = inet6addr_event
  605. };
  606. int __init roce_gid_mgmt_init(void)
  607. {
  608. register_inetaddr_notifier(&nb_inetaddr);
  609. if (IS_ENABLED(CONFIG_IPV6))
  610. register_inet6addr_notifier(&nb_inet6addr);
  611. /* We relay on the netdevice notifier to enumerate all
  612. * existing devices in the system. Register to this notifier
  613. * last to make sure we will not miss any IP add/del
  614. * callbacks.
  615. */
  616. register_netdevice_notifier(&nb_netdevice);
  617. return 0;
  618. }
  619. void __exit roce_gid_mgmt_cleanup(void)
  620. {
  621. if (IS_ENABLED(CONFIG_IPV6))
  622. unregister_inet6addr_notifier(&nb_inet6addr);
  623. unregister_inetaddr_notifier(&nb_inetaddr);
  624. unregister_netdevice_notifier(&nb_netdevice);
  625. /* Ensure all gid deletion tasks complete before we go down,
  626. * to avoid any reference to free'd memory. By the time
  627. * ib-core is removed, all physical devices have been removed,
  628. * so no issue with remaining hardware contexts.
  629. */
  630. }