roce_gid_mgmt.c 20 KB

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