switchdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * net/switchdev/switchdev.c - Switch device API
  3. * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
  4. * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/mutex.h>
  15. #include <linux/notifier.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/if_bridge.h>
  18. #include <net/ip_fib.h>
  19. #include <net/switchdev.h>
  20. /**
  21. * switchdev_port_attr_get - Get port attribute
  22. *
  23. * @dev: port device
  24. * @attr: attribute to get
  25. */
  26. int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
  27. {
  28. const struct switchdev_ops *ops = dev->switchdev_ops;
  29. struct net_device *lower_dev;
  30. struct list_head *iter;
  31. struct switchdev_attr first = {
  32. .id = SWITCHDEV_ATTR_UNDEFINED
  33. };
  34. int err = -EOPNOTSUPP;
  35. if (ops && ops->switchdev_port_attr_get)
  36. return ops->switchdev_port_attr_get(dev, attr);
  37. if (attr->flags & SWITCHDEV_F_NO_RECURSE)
  38. return err;
  39. /* Switch device port(s) may be stacked under
  40. * bond/team/vlan dev, so recurse down to get attr on
  41. * each port. Return -ENODATA if attr values don't
  42. * compare across ports.
  43. */
  44. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  45. err = switchdev_port_attr_get(lower_dev, attr);
  46. if (err)
  47. break;
  48. if (first.id == SWITCHDEV_ATTR_UNDEFINED)
  49. first = *attr;
  50. else if (memcmp(&first, attr, sizeof(*attr)))
  51. return -ENODATA;
  52. }
  53. return err;
  54. }
  55. EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
  56. static int __switchdev_port_attr_set(struct net_device *dev,
  57. struct switchdev_attr *attr)
  58. {
  59. const struct switchdev_ops *ops = dev->switchdev_ops;
  60. struct net_device *lower_dev;
  61. struct list_head *iter;
  62. int err = -EOPNOTSUPP;
  63. if (ops && ops->switchdev_port_attr_set)
  64. return ops->switchdev_port_attr_set(dev, attr);
  65. if (attr->flags & SWITCHDEV_F_NO_RECURSE)
  66. return err;
  67. /* Switch device port(s) may be stacked under
  68. * bond/team/vlan dev, so recurse down to set attr on
  69. * each port.
  70. */
  71. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  72. err = __switchdev_port_attr_set(lower_dev, attr);
  73. if (err)
  74. break;
  75. }
  76. return err;
  77. }
  78. struct switchdev_attr_set_work {
  79. struct work_struct work;
  80. struct net_device *dev;
  81. struct switchdev_attr attr;
  82. };
  83. static void switchdev_port_attr_set_work(struct work_struct *work)
  84. {
  85. struct switchdev_attr_set_work *asw =
  86. container_of(work, struct switchdev_attr_set_work, work);
  87. int err;
  88. rtnl_lock();
  89. err = switchdev_port_attr_set(asw->dev, &asw->attr);
  90. BUG_ON(err);
  91. rtnl_unlock();
  92. dev_put(asw->dev);
  93. kfree(work);
  94. }
  95. static int switchdev_port_attr_set_defer(struct net_device *dev,
  96. struct switchdev_attr *attr)
  97. {
  98. struct switchdev_attr_set_work *asw;
  99. asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
  100. if (!asw)
  101. return -ENOMEM;
  102. INIT_WORK(&asw->work, switchdev_port_attr_set_work);
  103. dev_hold(dev);
  104. asw->dev = dev;
  105. memcpy(&asw->attr, attr, sizeof(asw->attr));
  106. schedule_work(&asw->work);
  107. return 0;
  108. }
  109. /**
  110. * switchdev_port_attr_set - Set port attribute
  111. *
  112. * @dev: port device
  113. * @attr: attribute to set
  114. *
  115. * Use a 2-phase prepare-commit transaction model to ensure
  116. * system is not left in a partially updated state due to
  117. * failure from driver/device.
  118. */
  119. int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
  120. {
  121. int err;
  122. if (!rtnl_is_locked()) {
  123. /* Running prepare-commit transaction across stacked
  124. * devices requires nothing moves, so if rtnl_lock is
  125. * not held, schedule a worker thread to hold rtnl_lock
  126. * while setting attr.
  127. */
  128. return switchdev_port_attr_set_defer(dev, attr);
  129. }
  130. /* Phase I: prepare for attr set. Driver/device should fail
  131. * here if there are going to be issues in the commit phase,
  132. * such as lack of resources or support. The driver/device
  133. * should reserve resources needed for the commit phase here,
  134. * but should not commit the attr.
  135. */
  136. attr->trans = SWITCHDEV_TRANS_PREPARE;
  137. err = __switchdev_port_attr_set(dev, attr);
  138. if (err) {
  139. /* Prepare phase failed: abort the transaction. Any
  140. * resources reserved in the prepare phase are
  141. * released.
  142. */
  143. attr->trans = SWITCHDEV_TRANS_ABORT;
  144. __switchdev_port_attr_set(dev, attr);
  145. return err;
  146. }
  147. /* Phase II: commit attr set. This cannot fail as a fault
  148. * of driver/device. If it does, it's a bug in the driver/device
  149. * because the driver said everythings was OK in phase I.
  150. */
  151. attr->trans = SWITCHDEV_TRANS_COMMIT;
  152. err = __switchdev_port_attr_set(dev, attr);
  153. BUG_ON(err);
  154. return err;
  155. }
  156. EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
  157. int __switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
  158. {
  159. const struct switchdev_ops *ops = dev->switchdev_ops;
  160. struct net_device *lower_dev;
  161. struct list_head *iter;
  162. int err = -EOPNOTSUPP;
  163. if (ops && ops->switchdev_port_obj_add)
  164. return ops->switchdev_port_obj_add(dev, obj);
  165. /* Switch device port(s) may be stacked under
  166. * bond/team/vlan dev, so recurse down to add object on
  167. * each port.
  168. */
  169. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  170. err = __switchdev_port_obj_add(lower_dev, obj);
  171. if (err)
  172. break;
  173. }
  174. return err;
  175. }
  176. /**
  177. * switchdev_port_obj_add - Add port object
  178. *
  179. * @dev: port device
  180. * @obj: object to add
  181. *
  182. * Use a 2-phase prepare-commit transaction model to ensure
  183. * system is not left in a partially updated state due to
  184. * failure from driver/device.
  185. *
  186. * rtnl_lock must be held.
  187. */
  188. int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
  189. {
  190. int err;
  191. ASSERT_RTNL();
  192. /* Phase I: prepare for obj add. Driver/device should fail
  193. * here if there are going to be issues in the commit phase,
  194. * such as lack of resources or support. The driver/device
  195. * should reserve resources needed for the commit phase here,
  196. * but should not commit the obj.
  197. */
  198. obj->trans = SWITCHDEV_TRANS_PREPARE;
  199. err = __switchdev_port_obj_add(dev, obj);
  200. if (err) {
  201. /* Prepare phase failed: abort the transaction. Any
  202. * resources reserved in the prepare phase are
  203. * released.
  204. */
  205. obj->trans = SWITCHDEV_TRANS_ABORT;
  206. __switchdev_port_obj_add(dev, obj);
  207. return err;
  208. }
  209. /* Phase II: commit obj add. This cannot fail as a fault
  210. * of driver/device. If it does, it's a bug in the driver/device
  211. * because the driver said everythings was OK in phase I.
  212. */
  213. obj->trans = SWITCHDEV_TRANS_COMMIT;
  214. err = __switchdev_port_obj_add(dev, obj);
  215. WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
  216. return err;
  217. }
  218. EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
  219. /**
  220. * switchdev_port_obj_del - Delete port object
  221. *
  222. * @dev: port device
  223. * @obj: object to delete
  224. */
  225. int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
  226. {
  227. const struct switchdev_ops *ops = dev->switchdev_ops;
  228. struct net_device *lower_dev;
  229. struct list_head *iter;
  230. int err = -EOPNOTSUPP;
  231. if (ops && ops->switchdev_port_obj_del)
  232. return ops->switchdev_port_obj_del(dev, obj);
  233. /* Switch device port(s) may be stacked under
  234. * bond/team/vlan dev, so recurse down to delete object on
  235. * each port.
  236. */
  237. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  238. err = switchdev_port_obj_del(lower_dev, obj);
  239. if (err)
  240. break;
  241. }
  242. return err;
  243. }
  244. EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
  245. static DEFINE_MUTEX(switchdev_mutex);
  246. static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
  247. /**
  248. * register_switchdev_notifier - Register notifier
  249. * @nb: notifier_block
  250. *
  251. * Register switch device notifier. This should be used by code
  252. * which needs to monitor events happening in particular device.
  253. * Return values are same as for atomic_notifier_chain_register().
  254. */
  255. int register_switchdev_notifier(struct notifier_block *nb)
  256. {
  257. int err;
  258. mutex_lock(&switchdev_mutex);
  259. err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
  260. mutex_unlock(&switchdev_mutex);
  261. return err;
  262. }
  263. EXPORT_SYMBOL_GPL(register_switchdev_notifier);
  264. /**
  265. * unregister_switchdev_notifier - Unregister notifier
  266. * @nb: notifier_block
  267. *
  268. * Unregister switch device notifier.
  269. * Return values are same as for atomic_notifier_chain_unregister().
  270. */
  271. int unregister_switchdev_notifier(struct notifier_block *nb)
  272. {
  273. int err;
  274. mutex_lock(&switchdev_mutex);
  275. err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
  276. mutex_unlock(&switchdev_mutex);
  277. return err;
  278. }
  279. EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
  280. /**
  281. * call_switchdev_notifiers - Call notifiers
  282. * @val: value passed unmodified to notifier function
  283. * @dev: port device
  284. * @info: notifier information data
  285. *
  286. * Call all network notifier blocks. This should be called by driver
  287. * when it needs to propagate hardware event.
  288. * Return values are same as for atomic_notifier_call_chain().
  289. */
  290. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  291. struct switchdev_notifier_info *info)
  292. {
  293. int err;
  294. info->dev = dev;
  295. mutex_lock(&switchdev_mutex);
  296. err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
  297. mutex_unlock(&switchdev_mutex);
  298. return err;
  299. }
  300. EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
  301. static int switchdev_port_br_setflag(struct net_device *dev,
  302. struct nlattr *nlattr,
  303. unsigned long brport_flag)
  304. {
  305. struct switchdev_attr attr = {
  306. .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  307. };
  308. u8 flag = nla_get_u8(nlattr);
  309. int err;
  310. err = switchdev_port_attr_get(dev, &attr);
  311. if (err)
  312. return err;
  313. if (flag)
  314. attr.brport_flags |= brport_flag;
  315. else
  316. attr.brport_flags &= ~brport_flag;
  317. return switchdev_port_attr_set(dev, &attr);
  318. }
  319. static const struct nla_policy
  320. switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
  321. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  322. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  323. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  324. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  325. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  326. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  327. [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
  328. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  329. [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
  330. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  331. };
  332. static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
  333. struct nlattr *protinfo)
  334. {
  335. struct nlattr *attr;
  336. int rem;
  337. int err;
  338. err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
  339. switchdev_port_bridge_policy);
  340. if (err)
  341. return err;
  342. nla_for_each_nested(attr, protinfo, rem) {
  343. switch (nla_type(attr)) {
  344. case IFLA_BRPORT_LEARNING:
  345. err = switchdev_port_br_setflag(dev, attr,
  346. BR_LEARNING);
  347. break;
  348. case IFLA_BRPORT_LEARNING_SYNC:
  349. err = switchdev_port_br_setflag(dev, attr,
  350. BR_LEARNING_SYNC);
  351. break;
  352. default:
  353. err = -EOPNOTSUPP;
  354. break;
  355. }
  356. if (err)
  357. return err;
  358. }
  359. return 0;
  360. }
  361. static int switchdev_port_br_afspec(struct net_device *dev,
  362. struct nlattr *afspec,
  363. int (*f)(struct net_device *dev,
  364. struct switchdev_obj *obj))
  365. {
  366. struct nlattr *attr;
  367. struct bridge_vlan_info *vinfo;
  368. struct switchdev_obj obj = {
  369. .id = SWITCHDEV_OBJ_PORT_VLAN,
  370. };
  371. int rem;
  372. int err;
  373. nla_for_each_nested(attr, afspec, rem) {
  374. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  375. continue;
  376. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  377. return -EINVAL;
  378. vinfo = nla_data(attr);
  379. obj.vlan.flags = vinfo->flags;
  380. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  381. if (obj.vlan.vid_start)
  382. return -EINVAL;
  383. obj.vlan.vid_start = vinfo->vid;
  384. } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  385. if (!obj.vlan.vid_start)
  386. return -EINVAL;
  387. obj.vlan.vid_end = vinfo->vid;
  388. if (obj.vlan.vid_end <= obj.vlan.vid_start)
  389. return -EINVAL;
  390. err = f(dev, &obj);
  391. if (err)
  392. return err;
  393. memset(&obj.vlan, 0, sizeof(obj.vlan));
  394. } else {
  395. if (obj.vlan.vid_start)
  396. return -EINVAL;
  397. obj.vlan.vid_start = vinfo->vid;
  398. obj.vlan.vid_end = vinfo->vid;
  399. err = f(dev, &obj);
  400. if (err)
  401. return err;
  402. memset(&obj.vlan, 0, sizeof(obj.vlan));
  403. }
  404. }
  405. return 0;
  406. }
  407. /**
  408. * switchdev_port_bridge_setlink - Set bridge port attributes
  409. *
  410. * @dev: port device
  411. * @nlh: netlink header
  412. * @flags: netlink flags
  413. *
  414. * Called for SELF on rtnl_bridge_setlink to set bridge port
  415. * attributes.
  416. */
  417. int switchdev_port_bridge_setlink(struct net_device *dev,
  418. struct nlmsghdr *nlh, u16 flags)
  419. {
  420. struct nlattr *protinfo;
  421. struct nlattr *afspec;
  422. int err = 0;
  423. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  424. IFLA_PROTINFO);
  425. if (protinfo) {
  426. err = switchdev_port_br_setlink_protinfo(dev, protinfo);
  427. if (err)
  428. return err;
  429. }
  430. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  431. IFLA_AF_SPEC);
  432. if (afspec)
  433. err = switchdev_port_br_afspec(dev, afspec,
  434. switchdev_port_obj_add);
  435. return err;
  436. }
  437. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  438. /**
  439. * switchdev_port_bridge_dellink - Set bridge port attributes
  440. *
  441. * @dev: port device
  442. * @nlh: netlink header
  443. * @flags: netlink flags
  444. *
  445. * Called for SELF on rtnl_bridge_dellink to set bridge port
  446. * attributes.
  447. */
  448. int switchdev_port_bridge_dellink(struct net_device *dev,
  449. struct nlmsghdr *nlh, u16 flags)
  450. {
  451. struct nlattr *afspec;
  452. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  453. IFLA_AF_SPEC);
  454. if (afspec)
  455. return switchdev_port_br_afspec(dev, afspec,
  456. switchdev_port_obj_del);
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  460. /**
  461. * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
  462. * op for master devices
  463. *
  464. * @dev: port device
  465. * @nlh: netlink msg with bridge port attributes
  466. * @flags: bridge dellink flags
  467. *
  468. * Notify master device slaves of bridge port attribute deletes
  469. */
  470. int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
  471. struct nlmsghdr *nlh, u16 flags)
  472. {
  473. struct net_device *lower_dev;
  474. struct list_head *iter;
  475. int ret = 0, err = 0;
  476. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  477. return ret;
  478. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  479. err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
  480. if (err && err != -EOPNOTSUPP)
  481. ret = err;
  482. }
  483. return ret;
  484. }
  485. EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
  486. static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
  487. {
  488. const struct switchdev_ops *ops = dev->switchdev_ops;
  489. struct net_device *lower_dev;
  490. struct net_device *port_dev;
  491. struct list_head *iter;
  492. /* Recusively search down until we find a sw port dev.
  493. * (A sw port dev supports switchdev_port_attr_get).
  494. */
  495. if (ops && ops->switchdev_port_attr_get)
  496. return dev;
  497. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  498. port_dev = switchdev_get_lowest_dev(lower_dev);
  499. if (port_dev)
  500. return port_dev;
  501. }
  502. return NULL;
  503. }
  504. static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
  505. {
  506. struct switchdev_attr attr = {
  507. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  508. };
  509. struct switchdev_attr prev_attr;
  510. struct net_device *dev = NULL;
  511. int nhsel;
  512. /* For this route, all nexthop devs must be on the same switch. */
  513. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  514. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  515. if (!nh->nh_dev)
  516. return NULL;
  517. dev = switchdev_get_lowest_dev(nh->nh_dev);
  518. if (!dev)
  519. return NULL;
  520. if (switchdev_port_attr_get(dev, &attr))
  521. return NULL;
  522. if (nhsel > 0) {
  523. if (prev_attr.ppid.id_len != attr.ppid.id_len)
  524. return NULL;
  525. if (memcmp(prev_attr.ppid.id, attr.ppid.id,
  526. attr.ppid.id_len))
  527. return NULL;
  528. }
  529. prev_attr = attr;
  530. }
  531. return dev;
  532. }
  533. /**
  534. * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
  535. *
  536. * @dst: route's IPv4 destination address
  537. * @dst_len: destination address length (prefix length)
  538. * @fi: route FIB info structure
  539. * @tos: route TOS
  540. * @type: route type
  541. * @nlflags: netlink flags passed in (NLM_F_*)
  542. * @tb_id: route table ID
  543. *
  544. * Add IPv4 route entry to switch device.
  545. */
  546. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  547. u8 tos, u8 type, u32 nlflags, u32 tb_id)
  548. {
  549. struct net_device *dev;
  550. const struct switchdev_ops *ops;
  551. int err = 0;
  552. /* Don't offload route if using custom ip rules or if
  553. * IPv4 FIB offloading has been disabled completely.
  554. */
  555. #ifdef CONFIG_IP_MULTIPLE_TABLES
  556. if (fi->fib_net->ipv4.fib_has_custom_rules)
  557. return 0;
  558. #endif
  559. if (fi->fib_net->ipv4.fib_offload_disabled)
  560. return 0;
  561. dev = switchdev_get_dev_by_nhs(fi);
  562. if (!dev)
  563. return 0;
  564. ops = dev->switchdev_ops;
  565. if (ops->switchdev_fib_ipv4_add) {
  566. err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
  567. fi, tos, type, nlflags,
  568. tb_id);
  569. if (!err)
  570. fi->fib_flags |= RTNH_F_EXTERNAL;
  571. }
  572. return err;
  573. }
  574. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
  575. /**
  576. * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
  577. *
  578. * @dst: route's IPv4 destination address
  579. * @dst_len: destination address length (prefix length)
  580. * @fi: route FIB info structure
  581. * @tos: route TOS
  582. * @type: route type
  583. * @tb_id: route table ID
  584. *
  585. * Delete IPv4 route entry from switch device.
  586. */
  587. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  588. u8 tos, u8 type, u32 tb_id)
  589. {
  590. struct net_device *dev;
  591. const struct switchdev_ops *ops;
  592. int err = 0;
  593. if (!(fi->fib_flags & RTNH_F_EXTERNAL))
  594. return 0;
  595. dev = switchdev_get_dev_by_nhs(fi);
  596. if (!dev)
  597. return 0;
  598. ops = dev->switchdev_ops;
  599. if (ops->switchdev_fib_ipv4_del) {
  600. err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
  601. fi, tos, type, tb_id);
  602. if (!err)
  603. fi->fib_flags &= ~RTNH_F_EXTERNAL;
  604. }
  605. return err;
  606. }
  607. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
  608. /**
  609. * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
  610. *
  611. * @fi: route FIB info structure
  612. */
  613. void switchdev_fib_ipv4_abort(struct fib_info *fi)
  614. {
  615. /* There was a problem installing this route to the offload
  616. * device. For now, until we come up with more refined
  617. * policy handling, abruptly end IPv4 fib offloading for
  618. * for entire net by flushing offload device(s) of all
  619. * IPv4 routes, and mark IPv4 fib offloading broken from
  620. * this point forward.
  621. */
  622. fib_flush_external(fi->fib_net);
  623. fi->fib_net->ipv4.fib_offload_disabled = true;
  624. }
  625. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);