switchdev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 - Notify switch device port of bridge
  440. * port attribute delete
  441. *
  442. * @dev: port device
  443. * @nlh: netlink msg with bridge port attributes
  444. * @flags: bridge setlink flags
  445. *
  446. * Notify switch device port of bridge port attribute delete
  447. */
  448. int switchdev_port_bridge_dellink(struct net_device *dev,
  449. struct nlmsghdr *nlh, u16 flags)
  450. {
  451. const struct net_device_ops *ops = dev->netdev_ops;
  452. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  453. return 0;
  454. if (!ops->ndo_bridge_dellink)
  455. return -EOPNOTSUPP;
  456. return ops->ndo_bridge_dellink(dev, nlh, flags);
  457. }
  458. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  459. /**
  460. * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
  461. * op for master devices
  462. *
  463. * @dev: port device
  464. * @nlh: netlink msg with bridge port attributes
  465. * @flags: bridge setlink flags
  466. *
  467. * Notify master device slaves of bridge port attributes
  468. */
  469. int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
  470. struct nlmsghdr *nlh, u16 flags)
  471. {
  472. struct net_device *lower_dev;
  473. struct list_head *iter;
  474. int ret = 0, err = 0;
  475. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  476. return ret;
  477. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  478. err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
  479. if (err && err != -EOPNOTSUPP)
  480. ret = err;
  481. }
  482. return ret;
  483. }
  484. EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
  485. /**
  486. * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
  487. * op for master devices
  488. *
  489. * @dev: port device
  490. * @nlh: netlink msg with bridge port attributes
  491. * @flags: bridge dellink flags
  492. *
  493. * Notify master device slaves of bridge port attribute deletes
  494. */
  495. int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
  496. struct nlmsghdr *nlh, u16 flags)
  497. {
  498. struct net_device *lower_dev;
  499. struct list_head *iter;
  500. int ret = 0, err = 0;
  501. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  502. return ret;
  503. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  504. err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
  505. if (err && err != -EOPNOTSUPP)
  506. ret = err;
  507. }
  508. return ret;
  509. }
  510. EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
  511. static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
  512. {
  513. const struct switchdev_ops *ops = dev->switchdev_ops;
  514. struct net_device *lower_dev;
  515. struct net_device *port_dev;
  516. struct list_head *iter;
  517. /* Recusively search down until we find a sw port dev.
  518. * (A sw port dev supports switchdev_port_attr_get).
  519. */
  520. if (ops && ops->switchdev_port_attr_get)
  521. return dev;
  522. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  523. port_dev = switchdev_get_lowest_dev(lower_dev);
  524. if (port_dev)
  525. return port_dev;
  526. }
  527. return NULL;
  528. }
  529. static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
  530. {
  531. struct switchdev_attr attr = {
  532. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  533. };
  534. struct switchdev_attr prev_attr;
  535. struct net_device *dev = NULL;
  536. int nhsel;
  537. /* For this route, all nexthop devs must be on the same switch. */
  538. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  539. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  540. if (!nh->nh_dev)
  541. return NULL;
  542. dev = switchdev_get_lowest_dev(nh->nh_dev);
  543. if (!dev)
  544. return NULL;
  545. if (switchdev_port_attr_get(dev, &attr))
  546. return NULL;
  547. if (nhsel > 0) {
  548. if (prev_attr.ppid.id_len != attr.ppid.id_len)
  549. return NULL;
  550. if (memcmp(prev_attr.ppid.id, attr.ppid.id,
  551. attr.ppid.id_len))
  552. return NULL;
  553. }
  554. prev_attr = attr;
  555. }
  556. return dev;
  557. }
  558. /**
  559. * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
  560. *
  561. * @dst: route's IPv4 destination address
  562. * @dst_len: destination address length (prefix length)
  563. * @fi: route FIB info structure
  564. * @tos: route TOS
  565. * @type: route type
  566. * @nlflags: netlink flags passed in (NLM_F_*)
  567. * @tb_id: route table ID
  568. *
  569. * Add IPv4 route entry to switch device.
  570. */
  571. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  572. u8 tos, u8 type, u32 nlflags, u32 tb_id)
  573. {
  574. struct net_device *dev;
  575. const struct switchdev_ops *ops;
  576. int err = 0;
  577. /* Don't offload route if using custom ip rules or if
  578. * IPv4 FIB offloading has been disabled completely.
  579. */
  580. #ifdef CONFIG_IP_MULTIPLE_TABLES
  581. if (fi->fib_net->ipv4.fib_has_custom_rules)
  582. return 0;
  583. #endif
  584. if (fi->fib_net->ipv4.fib_offload_disabled)
  585. return 0;
  586. dev = switchdev_get_dev_by_nhs(fi);
  587. if (!dev)
  588. return 0;
  589. ops = dev->switchdev_ops;
  590. if (ops->switchdev_fib_ipv4_add) {
  591. err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
  592. fi, tos, type, nlflags,
  593. tb_id);
  594. if (!err)
  595. fi->fib_flags |= RTNH_F_EXTERNAL;
  596. }
  597. return err;
  598. }
  599. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
  600. /**
  601. * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
  602. *
  603. * @dst: route's IPv4 destination address
  604. * @dst_len: destination address length (prefix length)
  605. * @fi: route FIB info structure
  606. * @tos: route TOS
  607. * @type: route type
  608. * @tb_id: route table ID
  609. *
  610. * Delete IPv4 route entry from switch device.
  611. */
  612. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  613. u8 tos, u8 type, u32 tb_id)
  614. {
  615. struct net_device *dev;
  616. const struct switchdev_ops *ops;
  617. int err = 0;
  618. if (!(fi->fib_flags & RTNH_F_EXTERNAL))
  619. return 0;
  620. dev = switchdev_get_dev_by_nhs(fi);
  621. if (!dev)
  622. return 0;
  623. ops = dev->switchdev_ops;
  624. if (ops->switchdev_fib_ipv4_del) {
  625. err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
  626. fi, tos, type, tb_id);
  627. if (!err)
  628. fi->fib_flags &= ~RTNH_F_EXTERNAL;
  629. }
  630. return err;
  631. }
  632. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
  633. /**
  634. * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
  635. *
  636. * @fi: route FIB info structure
  637. */
  638. void switchdev_fib_ipv4_abort(struct fib_info *fi)
  639. {
  640. /* There was a problem installing this route to the offload
  641. * device. For now, until we come up with more refined
  642. * policy handling, abruptly end IPv4 fib offloading for
  643. * for entire net by flushing offload device(s) of all
  644. * IPv4 routes, and mark IPv4 fib offloading broken from
  645. * this point forward.
  646. */
  647. fib_flush_external(fi->fib_net);
  648. fi->fib_net->ipv4.fib_offload_disabled = true;
  649. }
  650. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);