switchdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. static int __switchdev_port_obj_add(struct net_device *dev,
  158. struct switchdev_obj *obj)
  159. {
  160. const struct switchdev_ops *ops = dev->switchdev_ops;
  161. struct net_device *lower_dev;
  162. struct list_head *iter;
  163. int err = -EOPNOTSUPP;
  164. if (ops && ops->switchdev_port_obj_add)
  165. return ops->switchdev_port_obj_add(dev, obj);
  166. /* Switch device port(s) may be stacked under
  167. * bond/team/vlan dev, so recurse down to add object on
  168. * each port.
  169. */
  170. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  171. err = __switchdev_port_obj_add(lower_dev, obj);
  172. if (err)
  173. break;
  174. }
  175. return err;
  176. }
  177. /**
  178. * switchdev_port_obj_add - Add port object
  179. *
  180. * @dev: port device
  181. * @obj: object to add
  182. *
  183. * Use a 2-phase prepare-commit transaction model to ensure
  184. * system is not left in a partially updated state due to
  185. * failure from driver/device.
  186. *
  187. * rtnl_lock must be held.
  188. */
  189. int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
  190. {
  191. int err;
  192. ASSERT_RTNL();
  193. /* Phase I: prepare for obj add. Driver/device should fail
  194. * here if there are going to be issues in the commit phase,
  195. * such as lack of resources or support. The driver/device
  196. * should reserve resources needed for the commit phase here,
  197. * but should not commit the obj.
  198. */
  199. obj->trans = SWITCHDEV_TRANS_PREPARE;
  200. err = __switchdev_port_obj_add(dev, obj);
  201. if (err) {
  202. /* Prepare phase failed: abort the transaction. Any
  203. * resources reserved in the prepare phase are
  204. * released.
  205. */
  206. obj->trans = SWITCHDEV_TRANS_ABORT;
  207. __switchdev_port_obj_add(dev, obj);
  208. return err;
  209. }
  210. /* Phase II: commit obj add. This cannot fail as a fault
  211. * of driver/device. If it does, it's a bug in the driver/device
  212. * because the driver said everythings was OK in phase I.
  213. */
  214. obj->trans = SWITCHDEV_TRANS_COMMIT;
  215. err = __switchdev_port_obj_add(dev, obj);
  216. WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
  217. return err;
  218. }
  219. EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
  220. /**
  221. * switchdev_port_obj_del - Delete port object
  222. *
  223. * @dev: port device
  224. * @obj: object to delete
  225. */
  226. int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
  227. {
  228. const struct switchdev_ops *ops = dev->switchdev_ops;
  229. struct net_device *lower_dev;
  230. struct list_head *iter;
  231. int err = -EOPNOTSUPP;
  232. if (ops && ops->switchdev_port_obj_del)
  233. return ops->switchdev_port_obj_del(dev, obj);
  234. /* Switch device port(s) may be stacked under
  235. * bond/team/vlan dev, so recurse down to delete object on
  236. * each port.
  237. */
  238. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  239. err = switchdev_port_obj_del(lower_dev, obj);
  240. if (err)
  241. break;
  242. }
  243. return err;
  244. }
  245. EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
  246. static DEFINE_MUTEX(switchdev_mutex);
  247. static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
  248. /**
  249. * register_switchdev_notifier - Register notifier
  250. * @nb: notifier_block
  251. *
  252. * Register switch device notifier. This should be used by code
  253. * which needs to monitor events happening in particular device.
  254. * Return values are same as for atomic_notifier_chain_register().
  255. */
  256. int register_switchdev_notifier(struct notifier_block *nb)
  257. {
  258. int err;
  259. mutex_lock(&switchdev_mutex);
  260. err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
  261. mutex_unlock(&switchdev_mutex);
  262. return err;
  263. }
  264. EXPORT_SYMBOL_GPL(register_switchdev_notifier);
  265. /**
  266. * unregister_switchdev_notifier - Unregister notifier
  267. * @nb: notifier_block
  268. *
  269. * Unregister switch device notifier.
  270. * Return values are same as for atomic_notifier_chain_unregister().
  271. */
  272. int unregister_switchdev_notifier(struct notifier_block *nb)
  273. {
  274. int err;
  275. mutex_lock(&switchdev_mutex);
  276. err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
  277. mutex_unlock(&switchdev_mutex);
  278. return err;
  279. }
  280. EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
  281. /**
  282. * call_switchdev_notifiers - Call notifiers
  283. * @val: value passed unmodified to notifier function
  284. * @dev: port device
  285. * @info: notifier information data
  286. *
  287. * Call all network notifier blocks. This should be called by driver
  288. * when it needs to propagate hardware event.
  289. * Return values are same as for atomic_notifier_call_chain().
  290. */
  291. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  292. struct switchdev_notifier_info *info)
  293. {
  294. int err;
  295. info->dev = dev;
  296. mutex_lock(&switchdev_mutex);
  297. err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
  298. mutex_unlock(&switchdev_mutex);
  299. return err;
  300. }
  301. EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
  302. /**
  303. * switchdev_port_bridge_getlink - Get bridge port attributes
  304. *
  305. * @dev: port device
  306. *
  307. * Called for SELF on rtnl_bridge_getlink to get bridge port
  308. * attributes.
  309. */
  310. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  311. struct net_device *dev, u32 filter_mask,
  312. int nlflags)
  313. {
  314. struct switchdev_attr attr = {
  315. .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  316. };
  317. u16 mode = BRIDGE_MODE_UNDEF;
  318. u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
  319. int err;
  320. err = switchdev_port_attr_get(dev, &attr);
  321. if (err)
  322. return err;
  323. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
  324. attr.u.brport_flags, mask, nlflags);
  325. }
  326. EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
  327. static int switchdev_port_br_setflag(struct net_device *dev,
  328. struct nlattr *nlattr,
  329. unsigned long brport_flag)
  330. {
  331. struct switchdev_attr attr = {
  332. .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  333. };
  334. u8 flag = nla_get_u8(nlattr);
  335. int err;
  336. err = switchdev_port_attr_get(dev, &attr);
  337. if (err)
  338. return err;
  339. if (flag)
  340. attr.u.brport_flags |= brport_flag;
  341. else
  342. attr.u.brport_flags &= ~brport_flag;
  343. return switchdev_port_attr_set(dev, &attr);
  344. }
  345. static const struct nla_policy
  346. switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
  347. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  348. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  349. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  350. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  351. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  352. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  353. [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
  354. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  355. [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
  356. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  357. };
  358. static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
  359. struct nlattr *protinfo)
  360. {
  361. struct nlattr *attr;
  362. int rem;
  363. int err;
  364. err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
  365. switchdev_port_bridge_policy);
  366. if (err)
  367. return err;
  368. nla_for_each_nested(attr, protinfo, rem) {
  369. switch (nla_type(attr)) {
  370. case IFLA_BRPORT_LEARNING:
  371. err = switchdev_port_br_setflag(dev, attr,
  372. BR_LEARNING);
  373. break;
  374. case IFLA_BRPORT_LEARNING_SYNC:
  375. err = switchdev_port_br_setflag(dev, attr,
  376. BR_LEARNING_SYNC);
  377. break;
  378. default:
  379. err = -EOPNOTSUPP;
  380. break;
  381. }
  382. if (err)
  383. return err;
  384. }
  385. return 0;
  386. }
  387. static int switchdev_port_br_afspec(struct net_device *dev,
  388. struct nlattr *afspec,
  389. int (*f)(struct net_device *dev,
  390. struct switchdev_obj *obj))
  391. {
  392. struct nlattr *attr;
  393. struct bridge_vlan_info *vinfo;
  394. struct switchdev_obj obj = {
  395. .id = SWITCHDEV_OBJ_PORT_VLAN,
  396. };
  397. struct switchdev_obj_vlan *vlan = &obj.u.vlan;
  398. int rem;
  399. int err;
  400. nla_for_each_nested(attr, afspec, rem) {
  401. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  402. continue;
  403. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  404. return -EINVAL;
  405. vinfo = nla_data(attr);
  406. vlan->flags = vinfo->flags;
  407. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  408. if (vlan->vid_start)
  409. return -EINVAL;
  410. vlan->vid_start = vinfo->vid;
  411. } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  412. if (!vlan->vid_start)
  413. return -EINVAL;
  414. vlan->vid_end = vinfo->vid;
  415. if (vlan->vid_end <= vlan->vid_start)
  416. return -EINVAL;
  417. err = f(dev, &obj);
  418. if (err)
  419. return err;
  420. memset(vlan, 0, sizeof(*vlan));
  421. } else {
  422. if (vlan->vid_start)
  423. return -EINVAL;
  424. vlan->vid_start = vinfo->vid;
  425. vlan->vid_end = vinfo->vid;
  426. err = f(dev, &obj);
  427. if (err)
  428. return err;
  429. memset(vlan, 0, sizeof(*vlan));
  430. }
  431. }
  432. return 0;
  433. }
  434. /**
  435. * switchdev_port_bridge_setlink - Set bridge port attributes
  436. *
  437. * @dev: port device
  438. * @nlh: netlink header
  439. * @flags: netlink flags
  440. *
  441. * Called for SELF on rtnl_bridge_setlink to set bridge port
  442. * attributes.
  443. */
  444. int switchdev_port_bridge_setlink(struct net_device *dev,
  445. struct nlmsghdr *nlh, u16 flags)
  446. {
  447. struct nlattr *protinfo;
  448. struct nlattr *afspec;
  449. int err = 0;
  450. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  451. IFLA_PROTINFO);
  452. if (protinfo) {
  453. err = switchdev_port_br_setlink_protinfo(dev, protinfo);
  454. if (err)
  455. return err;
  456. }
  457. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  458. IFLA_AF_SPEC);
  459. if (afspec)
  460. err = switchdev_port_br_afspec(dev, afspec,
  461. switchdev_port_obj_add);
  462. return err;
  463. }
  464. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  465. /**
  466. * switchdev_port_bridge_dellink - Set bridge port attributes
  467. *
  468. * @dev: port device
  469. * @nlh: netlink header
  470. * @flags: netlink flags
  471. *
  472. * Called for SELF on rtnl_bridge_dellink to set bridge port
  473. * attributes.
  474. */
  475. int switchdev_port_bridge_dellink(struct net_device *dev,
  476. struct nlmsghdr *nlh, u16 flags)
  477. {
  478. struct nlattr *afspec;
  479. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  480. IFLA_AF_SPEC);
  481. if (afspec)
  482. return switchdev_port_br_afspec(dev, afspec,
  483. switchdev_port_obj_del);
  484. return 0;
  485. }
  486. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  487. static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
  488. {
  489. const struct switchdev_ops *ops = dev->switchdev_ops;
  490. struct net_device *lower_dev;
  491. struct net_device *port_dev;
  492. struct list_head *iter;
  493. /* Recusively search down until we find a sw port dev.
  494. * (A sw port dev supports switchdev_port_attr_get).
  495. */
  496. if (ops && ops->switchdev_port_attr_get)
  497. return dev;
  498. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  499. port_dev = switchdev_get_lowest_dev(lower_dev);
  500. if (port_dev)
  501. return port_dev;
  502. }
  503. return NULL;
  504. }
  505. static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
  506. {
  507. struct switchdev_attr attr = {
  508. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  509. };
  510. struct switchdev_attr prev_attr;
  511. struct net_device *dev = NULL;
  512. int nhsel;
  513. /* For this route, all nexthop devs must be on the same switch. */
  514. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  515. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  516. if (!nh->nh_dev)
  517. return NULL;
  518. dev = switchdev_get_lowest_dev(nh->nh_dev);
  519. if (!dev)
  520. return NULL;
  521. if (switchdev_port_attr_get(dev, &attr))
  522. return NULL;
  523. if (nhsel > 0) {
  524. if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len)
  525. return NULL;
  526. if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id,
  527. attr.u.ppid.id_len))
  528. return NULL;
  529. }
  530. prev_attr = attr;
  531. }
  532. return dev;
  533. }
  534. /**
  535. * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
  536. *
  537. * @dst: route's IPv4 destination address
  538. * @dst_len: destination address length (prefix length)
  539. * @fi: route FIB info structure
  540. * @tos: route TOS
  541. * @type: route type
  542. * @nlflags: netlink flags passed in (NLM_F_*)
  543. * @tb_id: route table ID
  544. *
  545. * Add IPv4 route entry to switch device.
  546. */
  547. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  548. u8 tos, u8 type, u32 nlflags, u32 tb_id)
  549. {
  550. struct switchdev_obj fib_obj = {
  551. .id = SWITCHDEV_OBJ_IPV4_FIB,
  552. .u.ipv4_fib = {
  553. .dst = dst,
  554. .dst_len = dst_len,
  555. .fi = fi,
  556. .tos = tos,
  557. .type = type,
  558. .nlflags = nlflags,
  559. .tb_id = tb_id,
  560. },
  561. };
  562. struct net_device *dev;
  563. int err = 0;
  564. /* Don't offload route if using custom ip rules or if
  565. * IPv4 FIB offloading has been disabled completely.
  566. */
  567. #ifdef CONFIG_IP_MULTIPLE_TABLES
  568. if (fi->fib_net->ipv4.fib_has_custom_rules)
  569. return 0;
  570. #endif
  571. if (fi->fib_net->ipv4.fib_offload_disabled)
  572. return 0;
  573. dev = switchdev_get_dev_by_nhs(fi);
  574. if (!dev)
  575. return 0;
  576. err = switchdev_port_obj_add(dev, &fib_obj);
  577. if (!err)
  578. fi->fib_flags |= RTNH_F_EXTERNAL;
  579. return err;
  580. }
  581. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
  582. /**
  583. * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
  584. *
  585. * @dst: route's IPv4 destination address
  586. * @dst_len: destination address length (prefix length)
  587. * @fi: route FIB info structure
  588. * @tos: route TOS
  589. * @type: route type
  590. * @tb_id: route table ID
  591. *
  592. * Delete IPv4 route entry from switch device.
  593. */
  594. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  595. u8 tos, u8 type, u32 tb_id)
  596. {
  597. struct switchdev_obj fib_obj = {
  598. .id = SWITCHDEV_OBJ_IPV4_FIB,
  599. .u.ipv4_fib = {
  600. .dst = dst,
  601. .dst_len = dst_len,
  602. .fi = fi,
  603. .tos = tos,
  604. .type = type,
  605. .nlflags = 0,
  606. .tb_id = tb_id,
  607. },
  608. };
  609. struct net_device *dev;
  610. int err = 0;
  611. if (!(fi->fib_flags & RTNH_F_EXTERNAL))
  612. return 0;
  613. dev = switchdev_get_dev_by_nhs(fi);
  614. if (!dev)
  615. return 0;
  616. err = switchdev_port_obj_del(dev, &fib_obj);
  617. if (!err)
  618. fi->fib_flags &= ~RTNH_F_EXTERNAL;
  619. return err;
  620. }
  621. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
  622. /**
  623. * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
  624. *
  625. * @fi: route FIB info structure
  626. */
  627. void switchdev_fib_ipv4_abort(struct fib_info *fi)
  628. {
  629. /* There was a problem installing this route to the offload
  630. * device. For now, until we come up with more refined
  631. * policy handling, abruptly end IPv4 fib offloading for
  632. * for entire net by flushing offload device(s) of all
  633. * IPv4 routes, and mark IPv4 fib offloading broken from
  634. * this point forward.
  635. */
  636. fib_flush_external(fi->fib_net);
  637. fi->fib_net->ipv4.fib_offload_disabled = true;
  638. }
  639. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);