switchdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. /**
  302. * switchdev_port_bridge_getlink - Get bridge port attributes
  303. *
  304. * @dev: port device
  305. *
  306. * Called for SELF on rtnl_bridge_getlink to get bridge port
  307. * attributes.
  308. */
  309. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  310. struct net_device *dev, u32 filter_mask,
  311. int nlflags)
  312. {
  313. struct switchdev_attr attr = {
  314. .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  315. };
  316. u16 mode = BRIDGE_MODE_UNDEF;
  317. u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
  318. int err;
  319. err = switchdev_port_attr_get(dev, &attr);
  320. if (err)
  321. return err;
  322. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
  323. attr.brport_flags, mask, nlflags);
  324. }
  325. EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
  326. static int switchdev_port_br_setflag(struct net_device *dev,
  327. struct nlattr *nlattr,
  328. unsigned long brport_flag)
  329. {
  330. struct switchdev_attr attr = {
  331. .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  332. };
  333. u8 flag = nla_get_u8(nlattr);
  334. int err;
  335. err = switchdev_port_attr_get(dev, &attr);
  336. if (err)
  337. return err;
  338. if (flag)
  339. attr.brport_flags |= brport_flag;
  340. else
  341. attr.brport_flags &= ~brport_flag;
  342. return switchdev_port_attr_set(dev, &attr);
  343. }
  344. static const struct nla_policy
  345. switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
  346. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  347. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  348. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  349. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  350. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  351. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  352. [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
  353. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  354. [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
  355. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  356. };
  357. static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
  358. struct nlattr *protinfo)
  359. {
  360. struct nlattr *attr;
  361. int rem;
  362. int err;
  363. err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
  364. switchdev_port_bridge_policy);
  365. if (err)
  366. return err;
  367. nla_for_each_nested(attr, protinfo, rem) {
  368. switch (nla_type(attr)) {
  369. case IFLA_BRPORT_LEARNING:
  370. err = switchdev_port_br_setflag(dev, attr,
  371. BR_LEARNING);
  372. break;
  373. case IFLA_BRPORT_LEARNING_SYNC:
  374. err = switchdev_port_br_setflag(dev, attr,
  375. BR_LEARNING_SYNC);
  376. break;
  377. default:
  378. err = -EOPNOTSUPP;
  379. break;
  380. }
  381. if (err)
  382. return err;
  383. }
  384. return 0;
  385. }
  386. static int switchdev_port_br_afspec(struct net_device *dev,
  387. struct nlattr *afspec,
  388. int (*f)(struct net_device *dev,
  389. struct switchdev_obj *obj))
  390. {
  391. struct nlattr *attr;
  392. struct bridge_vlan_info *vinfo;
  393. struct switchdev_obj obj = {
  394. .id = SWITCHDEV_OBJ_PORT_VLAN,
  395. };
  396. int rem;
  397. int err;
  398. nla_for_each_nested(attr, afspec, rem) {
  399. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  400. continue;
  401. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  402. return -EINVAL;
  403. vinfo = nla_data(attr);
  404. obj.vlan.flags = vinfo->flags;
  405. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  406. if (obj.vlan.vid_start)
  407. return -EINVAL;
  408. obj.vlan.vid_start = vinfo->vid;
  409. } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  410. if (!obj.vlan.vid_start)
  411. return -EINVAL;
  412. obj.vlan.vid_end = vinfo->vid;
  413. if (obj.vlan.vid_end <= obj.vlan.vid_start)
  414. return -EINVAL;
  415. err = f(dev, &obj);
  416. if (err)
  417. return err;
  418. memset(&obj.vlan, 0, sizeof(obj.vlan));
  419. } else {
  420. if (obj.vlan.vid_start)
  421. return -EINVAL;
  422. obj.vlan.vid_start = vinfo->vid;
  423. obj.vlan.vid_end = vinfo->vid;
  424. err = f(dev, &obj);
  425. if (err)
  426. return err;
  427. memset(&obj.vlan, 0, sizeof(obj.vlan));
  428. }
  429. }
  430. return 0;
  431. }
  432. /**
  433. * switchdev_port_bridge_setlink - Set bridge port attributes
  434. *
  435. * @dev: port device
  436. * @nlh: netlink header
  437. * @flags: netlink flags
  438. *
  439. * Called for SELF on rtnl_bridge_setlink to set bridge port
  440. * attributes.
  441. */
  442. int switchdev_port_bridge_setlink(struct net_device *dev,
  443. struct nlmsghdr *nlh, u16 flags)
  444. {
  445. struct nlattr *protinfo;
  446. struct nlattr *afspec;
  447. int err = 0;
  448. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  449. IFLA_PROTINFO);
  450. if (protinfo) {
  451. err = switchdev_port_br_setlink_protinfo(dev, protinfo);
  452. if (err)
  453. return err;
  454. }
  455. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  456. IFLA_AF_SPEC);
  457. if (afspec)
  458. err = switchdev_port_br_afspec(dev, afspec,
  459. switchdev_port_obj_add);
  460. return err;
  461. }
  462. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  463. /**
  464. * switchdev_port_bridge_dellink - Set bridge port attributes
  465. *
  466. * @dev: port device
  467. * @nlh: netlink header
  468. * @flags: netlink flags
  469. *
  470. * Called for SELF on rtnl_bridge_dellink to set bridge port
  471. * attributes.
  472. */
  473. int switchdev_port_bridge_dellink(struct net_device *dev,
  474. struct nlmsghdr *nlh, u16 flags)
  475. {
  476. struct nlattr *afspec;
  477. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  478. IFLA_AF_SPEC);
  479. if (afspec)
  480. return switchdev_port_br_afspec(dev, afspec,
  481. switchdev_port_obj_del);
  482. return 0;
  483. }
  484. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  485. static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
  486. {
  487. const struct switchdev_ops *ops = dev->switchdev_ops;
  488. struct net_device *lower_dev;
  489. struct net_device *port_dev;
  490. struct list_head *iter;
  491. /* Recusively search down until we find a sw port dev.
  492. * (A sw port dev supports switchdev_port_attr_get).
  493. */
  494. if (ops && ops->switchdev_port_attr_get)
  495. return dev;
  496. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  497. port_dev = switchdev_get_lowest_dev(lower_dev);
  498. if (port_dev)
  499. return port_dev;
  500. }
  501. return NULL;
  502. }
  503. static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
  504. {
  505. struct switchdev_attr attr = {
  506. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  507. };
  508. struct switchdev_attr prev_attr;
  509. struct net_device *dev = NULL;
  510. int nhsel;
  511. /* For this route, all nexthop devs must be on the same switch. */
  512. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  513. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  514. if (!nh->nh_dev)
  515. return NULL;
  516. dev = switchdev_get_lowest_dev(nh->nh_dev);
  517. if (!dev)
  518. return NULL;
  519. if (switchdev_port_attr_get(dev, &attr))
  520. return NULL;
  521. if (nhsel > 0) {
  522. if (prev_attr.ppid.id_len != attr.ppid.id_len)
  523. return NULL;
  524. if (memcmp(prev_attr.ppid.id, attr.ppid.id,
  525. attr.ppid.id_len))
  526. return NULL;
  527. }
  528. prev_attr = attr;
  529. }
  530. return dev;
  531. }
  532. /**
  533. * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
  534. *
  535. * @dst: route's IPv4 destination address
  536. * @dst_len: destination address length (prefix length)
  537. * @fi: route FIB info structure
  538. * @tos: route TOS
  539. * @type: route type
  540. * @nlflags: netlink flags passed in (NLM_F_*)
  541. * @tb_id: route table ID
  542. *
  543. * Add IPv4 route entry to switch device.
  544. */
  545. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  546. u8 tos, u8 type, u32 nlflags, u32 tb_id)
  547. {
  548. struct switchdev_obj fib_obj = {
  549. .id = SWITCHDEV_OBJ_IPV4_FIB,
  550. .ipv4_fib = {
  551. .dst = htonl(dst),
  552. .dst_len = dst_len,
  553. .fi = fi,
  554. .tos = tos,
  555. .type = type,
  556. .nlflags = nlflags,
  557. .tb_id = tb_id,
  558. },
  559. };
  560. struct net_device *dev;
  561. int err = 0;
  562. /* Don't offload route if using custom ip rules or if
  563. * IPv4 FIB offloading has been disabled completely.
  564. */
  565. #ifdef CONFIG_IP_MULTIPLE_TABLES
  566. if (fi->fib_net->ipv4.fib_has_custom_rules)
  567. return 0;
  568. #endif
  569. if (fi->fib_net->ipv4.fib_offload_disabled)
  570. return 0;
  571. dev = switchdev_get_dev_by_nhs(fi);
  572. if (!dev)
  573. return 0;
  574. err = switchdev_port_obj_add(dev, &fib_obj);
  575. if (!err)
  576. fi->fib_flags |= RTNH_F_EXTERNAL;
  577. return err;
  578. }
  579. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
  580. /**
  581. * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
  582. *
  583. * @dst: route's IPv4 destination address
  584. * @dst_len: destination address length (prefix length)
  585. * @fi: route FIB info structure
  586. * @tos: route TOS
  587. * @type: route type
  588. * @tb_id: route table ID
  589. *
  590. * Delete IPv4 route entry from switch device.
  591. */
  592. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  593. u8 tos, u8 type, u32 tb_id)
  594. {
  595. struct switchdev_obj fib_obj = {
  596. .id = SWITCHDEV_OBJ_IPV4_FIB,
  597. .ipv4_fib = {
  598. .dst = htonl(dst),
  599. .dst_len = dst_len,
  600. .fi = fi,
  601. .tos = tos,
  602. .type = type,
  603. .nlflags = 0,
  604. .tb_id = tb_id,
  605. },
  606. };
  607. struct net_device *dev;
  608. int err = 0;
  609. if (!(fi->fib_flags & RTNH_F_EXTERNAL))
  610. return 0;
  611. dev = switchdev_get_dev_by_nhs(fi);
  612. if (!dev)
  613. return 0;
  614. err = switchdev_port_obj_del(dev, &fib_obj);
  615. if (!err)
  616. fi->fib_flags &= ~RTNH_F_EXTERNAL;
  617. return err;
  618. }
  619. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
  620. /**
  621. * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
  622. *
  623. * @fi: route FIB info structure
  624. */
  625. void switchdev_fib_ipv4_abort(struct fib_info *fi)
  626. {
  627. /* There was a problem installing this route to the offload
  628. * device. For now, until we come up with more refined
  629. * policy handling, abruptly end IPv4 fib offloading for
  630. * for entire net by flushing offload device(s) of all
  631. * IPv4 routes, and mark IPv4 fib offloading broken from
  632. * this point forward.
  633. */
  634. fib_flush_external(fi->fib_net);
  635. fi->fib_net->ipv4.fib_offload_disabled = true;
  636. }
  637. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);