switchdev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. if (err && err != -EOPNOTSUPP)
  91. netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
  92. err, asw->attr.id);
  93. rtnl_unlock();
  94. dev_put(asw->dev);
  95. kfree(work);
  96. }
  97. static int switchdev_port_attr_set_defer(struct net_device *dev,
  98. struct switchdev_attr *attr)
  99. {
  100. struct switchdev_attr_set_work *asw;
  101. asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
  102. if (!asw)
  103. return -ENOMEM;
  104. INIT_WORK(&asw->work, switchdev_port_attr_set_work);
  105. dev_hold(dev);
  106. asw->dev = dev;
  107. memcpy(&asw->attr, attr, sizeof(asw->attr));
  108. schedule_work(&asw->work);
  109. return 0;
  110. }
  111. /**
  112. * switchdev_port_attr_set - Set port attribute
  113. *
  114. * @dev: port device
  115. * @attr: attribute to set
  116. *
  117. * Use a 2-phase prepare-commit transaction model to ensure
  118. * system is not left in a partially updated state due to
  119. * failure from driver/device.
  120. */
  121. int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
  122. {
  123. int err;
  124. if (!rtnl_is_locked()) {
  125. /* Running prepare-commit transaction across stacked
  126. * devices requires nothing moves, so if rtnl_lock is
  127. * not held, schedule a worker thread to hold rtnl_lock
  128. * while setting attr.
  129. */
  130. return switchdev_port_attr_set_defer(dev, attr);
  131. }
  132. /* Phase I: prepare for attr set. Driver/device should fail
  133. * here if there are going to be issues in the commit phase,
  134. * such as lack of resources or support. The driver/device
  135. * should reserve resources needed for the commit phase here,
  136. * but should not commit the attr.
  137. */
  138. attr->trans = SWITCHDEV_TRANS_PREPARE;
  139. err = __switchdev_port_attr_set(dev, attr);
  140. if (err) {
  141. /* Prepare phase failed: abort the transaction. Any
  142. * resources reserved in the prepare phase are
  143. * released.
  144. */
  145. attr->trans = SWITCHDEV_TRANS_ABORT;
  146. __switchdev_port_attr_set(dev, attr);
  147. return err;
  148. }
  149. /* Phase II: commit attr set. This cannot fail as a fault
  150. * of driver/device. If it does, it's a bug in the driver/device
  151. * because the driver said everythings was OK in phase I.
  152. */
  153. attr->trans = SWITCHDEV_TRANS_COMMIT;
  154. err = __switchdev_port_attr_set(dev, attr);
  155. BUG_ON(err);
  156. return err;
  157. }
  158. EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
  159. static int __switchdev_port_obj_add(struct net_device *dev,
  160. struct switchdev_obj *obj)
  161. {
  162. const struct switchdev_ops *ops = dev->switchdev_ops;
  163. struct net_device *lower_dev;
  164. struct list_head *iter;
  165. int err = -EOPNOTSUPP;
  166. if (ops && ops->switchdev_port_obj_add)
  167. return ops->switchdev_port_obj_add(dev, obj);
  168. /* Switch device port(s) may be stacked under
  169. * bond/team/vlan dev, so recurse down to add object on
  170. * each port.
  171. */
  172. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  173. err = __switchdev_port_obj_add(lower_dev, obj);
  174. if (err)
  175. break;
  176. }
  177. return err;
  178. }
  179. /**
  180. * switchdev_port_obj_add - Add port object
  181. *
  182. * @dev: port device
  183. * @obj: object to add
  184. *
  185. * Use a 2-phase prepare-commit transaction model to ensure
  186. * system is not left in a partially updated state due to
  187. * failure from driver/device.
  188. *
  189. * rtnl_lock must be held.
  190. */
  191. int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
  192. {
  193. int err;
  194. ASSERT_RTNL();
  195. /* Phase I: prepare for obj add. Driver/device should fail
  196. * here if there are going to be issues in the commit phase,
  197. * such as lack of resources or support. The driver/device
  198. * should reserve resources needed for the commit phase here,
  199. * but should not commit the obj.
  200. */
  201. obj->trans = SWITCHDEV_TRANS_PREPARE;
  202. err = __switchdev_port_obj_add(dev, obj);
  203. if (err) {
  204. /* Prepare phase failed: abort the transaction. Any
  205. * resources reserved in the prepare phase are
  206. * released.
  207. */
  208. obj->trans = SWITCHDEV_TRANS_ABORT;
  209. __switchdev_port_obj_add(dev, obj);
  210. return err;
  211. }
  212. /* Phase II: commit obj add. This cannot fail as a fault
  213. * of driver/device. If it does, it's a bug in the driver/device
  214. * because the driver said everythings was OK in phase I.
  215. */
  216. obj->trans = SWITCHDEV_TRANS_COMMIT;
  217. err = __switchdev_port_obj_add(dev, obj);
  218. WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
  219. return err;
  220. }
  221. EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
  222. /**
  223. * switchdev_port_obj_del - Delete port object
  224. *
  225. * @dev: port device
  226. * @obj: object to delete
  227. */
  228. int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
  229. {
  230. const struct switchdev_ops *ops = dev->switchdev_ops;
  231. struct net_device *lower_dev;
  232. struct list_head *iter;
  233. int err = -EOPNOTSUPP;
  234. if (ops && ops->switchdev_port_obj_del)
  235. return ops->switchdev_port_obj_del(dev, obj);
  236. /* Switch device port(s) may be stacked under
  237. * bond/team/vlan dev, so recurse down to delete object on
  238. * each port.
  239. */
  240. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  241. err = switchdev_port_obj_del(lower_dev, obj);
  242. if (err)
  243. break;
  244. }
  245. return err;
  246. }
  247. EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
  248. /**
  249. * switchdev_port_obj_dump - Dump port objects
  250. *
  251. * @dev: port device
  252. * @obj: object to dump
  253. */
  254. int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj)
  255. {
  256. const struct switchdev_ops *ops = dev->switchdev_ops;
  257. struct net_device *lower_dev;
  258. struct list_head *iter;
  259. int err = -EOPNOTSUPP;
  260. if (ops && ops->switchdev_port_obj_dump)
  261. return ops->switchdev_port_obj_dump(dev, obj);
  262. /* Switch device port(s) may be stacked under
  263. * bond/team/vlan dev, so recurse down to dump objects on
  264. * first port at bottom of stack.
  265. */
  266. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  267. err = switchdev_port_obj_dump(lower_dev, obj);
  268. break;
  269. }
  270. return err;
  271. }
  272. EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
  273. static DEFINE_MUTEX(switchdev_mutex);
  274. static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
  275. /**
  276. * register_switchdev_notifier - Register notifier
  277. * @nb: notifier_block
  278. *
  279. * Register switch device notifier. This should be used by code
  280. * which needs to monitor events happening in particular device.
  281. * Return values are same as for atomic_notifier_chain_register().
  282. */
  283. int register_switchdev_notifier(struct notifier_block *nb)
  284. {
  285. int err;
  286. mutex_lock(&switchdev_mutex);
  287. err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
  288. mutex_unlock(&switchdev_mutex);
  289. return err;
  290. }
  291. EXPORT_SYMBOL_GPL(register_switchdev_notifier);
  292. /**
  293. * unregister_switchdev_notifier - Unregister notifier
  294. * @nb: notifier_block
  295. *
  296. * Unregister switch device notifier.
  297. * Return values are same as for atomic_notifier_chain_unregister().
  298. */
  299. int unregister_switchdev_notifier(struct notifier_block *nb)
  300. {
  301. int err;
  302. mutex_lock(&switchdev_mutex);
  303. err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
  304. mutex_unlock(&switchdev_mutex);
  305. return err;
  306. }
  307. EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
  308. /**
  309. * call_switchdev_notifiers - Call notifiers
  310. * @val: value passed unmodified to notifier function
  311. * @dev: port device
  312. * @info: notifier information data
  313. *
  314. * Call all network notifier blocks. This should be called by driver
  315. * when it needs to propagate hardware event.
  316. * Return values are same as for atomic_notifier_call_chain().
  317. */
  318. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  319. struct switchdev_notifier_info *info)
  320. {
  321. int err;
  322. info->dev = dev;
  323. mutex_lock(&switchdev_mutex);
  324. err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
  325. mutex_unlock(&switchdev_mutex);
  326. return err;
  327. }
  328. EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
  329. /**
  330. * switchdev_port_bridge_getlink - Get bridge port attributes
  331. *
  332. * @dev: port device
  333. *
  334. * Called for SELF on rtnl_bridge_getlink to get bridge port
  335. * attributes.
  336. */
  337. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  338. struct net_device *dev, u32 filter_mask,
  339. int nlflags)
  340. {
  341. struct switchdev_attr attr = {
  342. .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  343. };
  344. u16 mode = BRIDGE_MODE_UNDEF;
  345. u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
  346. int err;
  347. err = switchdev_port_attr_get(dev, &attr);
  348. if (err)
  349. return err;
  350. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
  351. attr.u.brport_flags, mask, nlflags);
  352. }
  353. EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
  354. static int switchdev_port_br_setflag(struct net_device *dev,
  355. struct nlattr *nlattr,
  356. unsigned long brport_flag)
  357. {
  358. struct switchdev_attr attr = {
  359. .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  360. };
  361. u8 flag = nla_get_u8(nlattr);
  362. int err;
  363. err = switchdev_port_attr_get(dev, &attr);
  364. if (err)
  365. return err;
  366. if (flag)
  367. attr.u.brport_flags |= brport_flag;
  368. else
  369. attr.u.brport_flags &= ~brport_flag;
  370. return switchdev_port_attr_set(dev, &attr);
  371. }
  372. static const struct nla_policy
  373. switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
  374. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  375. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  376. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  377. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  378. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  379. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  380. [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
  381. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  382. [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
  383. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  384. };
  385. static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
  386. struct nlattr *protinfo)
  387. {
  388. struct nlattr *attr;
  389. int rem;
  390. int err;
  391. err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
  392. switchdev_port_bridge_policy);
  393. if (err)
  394. return err;
  395. nla_for_each_nested(attr, protinfo, rem) {
  396. switch (nla_type(attr)) {
  397. case IFLA_BRPORT_LEARNING:
  398. err = switchdev_port_br_setflag(dev, attr,
  399. BR_LEARNING);
  400. break;
  401. case IFLA_BRPORT_LEARNING_SYNC:
  402. err = switchdev_port_br_setflag(dev, attr,
  403. BR_LEARNING_SYNC);
  404. break;
  405. default:
  406. err = -EOPNOTSUPP;
  407. break;
  408. }
  409. if (err)
  410. return err;
  411. }
  412. return 0;
  413. }
  414. static int switchdev_port_br_afspec(struct net_device *dev,
  415. struct nlattr *afspec,
  416. int (*f)(struct net_device *dev,
  417. struct switchdev_obj *obj))
  418. {
  419. struct nlattr *attr;
  420. struct bridge_vlan_info *vinfo;
  421. struct switchdev_obj obj = {
  422. .id = SWITCHDEV_OBJ_PORT_VLAN,
  423. };
  424. struct switchdev_obj_vlan *vlan = &obj.u.vlan;
  425. int rem;
  426. int err;
  427. nla_for_each_nested(attr, afspec, rem) {
  428. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  429. continue;
  430. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  431. return -EINVAL;
  432. vinfo = nla_data(attr);
  433. vlan->flags = vinfo->flags;
  434. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  435. if (vlan->vid_start)
  436. return -EINVAL;
  437. vlan->vid_start = vinfo->vid;
  438. } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  439. if (!vlan->vid_start)
  440. return -EINVAL;
  441. vlan->vid_end = vinfo->vid;
  442. if (vlan->vid_end <= vlan->vid_start)
  443. return -EINVAL;
  444. err = f(dev, &obj);
  445. if (err)
  446. return err;
  447. memset(vlan, 0, sizeof(*vlan));
  448. } else {
  449. if (vlan->vid_start)
  450. return -EINVAL;
  451. vlan->vid_start = vinfo->vid;
  452. vlan->vid_end = vinfo->vid;
  453. err = f(dev, &obj);
  454. if (err)
  455. return err;
  456. memset(vlan, 0, sizeof(*vlan));
  457. }
  458. }
  459. return 0;
  460. }
  461. /**
  462. * switchdev_port_bridge_setlink - Set bridge port attributes
  463. *
  464. * @dev: port device
  465. * @nlh: netlink header
  466. * @flags: netlink flags
  467. *
  468. * Called for SELF on rtnl_bridge_setlink to set bridge port
  469. * attributes.
  470. */
  471. int switchdev_port_bridge_setlink(struct net_device *dev,
  472. struct nlmsghdr *nlh, u16 flags)
  473. {
  474. struct nlattr *protinfo;
  475. struct nlattr *afspec;
  476. int err = 0;
  477. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  478. IFLA_PROTINFO);
  479. if (protinfo) {
  480. err = switchdev_port_br_setlink_protinfo(dev, protinfo);
  481. if (err)
  482. return err;
  483. }
  484. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  485. IFLA_AF_SPEC);
  486. if (afspec)
  487. err = switchdev_port_br_afspec(dev, afspec,
  488. switchdev_port_obj_add);
  489. return err;
  490. }
  491. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  492. /**
  493. * switchdev_port_bridge_dellink - Set bridge port attributes
  494. *
  495. * @dev: port device
  496. * @nlh: netlink header
  497. * @flags: netlink flags
  498. *
  499. * Called for SELF on rtnl_bridge_dellink to set bridge port
  500. * attributes.
  501. */
  502. int switchdev_port_bridge_dellink(struct net_device *dev,
  503. struct nlmsghdr *nlh, u16 flags)
  504. {
  505. struct nlattr *afspec;
  506. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  507. IFLA_AF_SPEC);
  508. if (afspec)
  509. return switchdev_port_br_afspec(dev, afspec,
  510. switchdev_port_obj_del);
  511. return 0;
  512. }
  513. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  514. /**
  515. * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
  516. *
  517. * @ndmsg: netlink hdr
  518. * @nlattr: netlink attributes
  519. * @dev: port device
  520. * @addr: MAC address to add
  521. * @vid: VLAN to add
  522. *
  523. * Add FDB entry to switch device.
  524. */
  525. int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  526. struct net_device *dev, const unsigned char *addr,
  527. u16 vid, u16 nlm_flags)
  528. {
  529. struct switchdev_obj obj = {
  530. .id = SWITCHDEV_OBJ_PORT_FDB,
  531. .u.fdb = {
  532. .addr = addr,
  533. .vid = vid,
  534. },
  535. };
  536. return switchdev_port_obj_add(dev, &obj);
  537. }
  538. EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
  539. /**
  540. * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
  541. *
  542. * @ndmsg: netlink hdr
  543. * @nlattr: netlink attributes
  544. * @dev: port device
  545. * @addr: MAC address to delete
  546. * @vid: VLAN to delete
  547. *
  548. * Delete FDB entry from switch device.
  549. */
  550. int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  551. struct net_device *dev, const unsigned char *addr,
  552. u16 vid)
  553. {
  554. struct switchdev_obj obj = {
  555. .id = SWITCHDEV_OBJ_PORT_FDB,
  556. .u.fdb = {
  557. .addr = addr,
  558. .vid = vid,
  559. },
  560. };
  561. return switchdev_port_obj_del(dev, &obj);
  562. }
  563. EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
  564. struct switchdev_fdb_dump {
  565. struct switchdev_obj obj;
  566. struct sk_buff *skb;
  567. struct netlink_callback *cb;
  568. struct net_device *filter_dev;
  569. int idx;
  570. };
  571. static int switchdev_port_fdb_dump_cb(struct net_device *dev,
  572. struct switchdev_obj *obj)
  573. {
  574. struct switchdev_fdb_dump *dump =
  575. container_of(obj, struct switchdev_fdb_dump, obj);
  576. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  577. u32 seq = dump->cb->nlh->nlmsg_seq;
  578. struct nlmsghdr *nlh;
  579. struct ndmsg *ndm;
  580. struct net_device *master = netdev_master_upper_dev_get(dev);
  581. if (dump->idx < dump->cb->args[0])
  582. goto skip;
  583. if (master && dump->filter_dev != master)
  584. goto skip;
  585. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  586. sizeof(*ndm), NLM_F_MULTI);
  587. if (!nlh)
  588. return -EMSGSIZE;
  589. ndm = nlmsg_data(nlh);
  590. ndm->ndm_family = AF_BRIDGE;
  591. ndm->ndm_pad1 = 0;
  592. ndm->ndm_pad2 = 0;
  593. ndm->ndm_flags = NTF_SELF;
  594. ndm->ndm_type = 0;
  595. ndm->ndm_ifindex = dev->ifindex;
  596. ndm->ndm_state = NUD_REACHABLE;
  597. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr))
  598. goto nla_put_failure;
  599. if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid))
  600. goto nla_put_failure;
  601. nlmsg_end(dump->skb, nlh);
  602. skip:
  603. dump->idx++;
  604. return 0;
  605. nla_put_failure:
  606. nlmsg_cancel(dump->skb, nlh);
  607. return -EMSGSIZE;
  608. }
  609. /**
  610. * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
  611. *
  612. * @skb: netlink skb
  613. * @cb: netlink callback
  614. * @dev: port device
  615. * @filter_dev: filter device
  616. * @idx:
  617. *
  618. * Delete FDB entry from switch device.
  619. */
  620. int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  621. struct net_device *dev,
  622. struct net_device *filter_dev, int idx)
  623. {
  624. struct switchdev_fdb_dump dump = {
  625. .obj = {
  626. .id = SWITCHDEV_OBJ_PORT_FDB,
  627. .cb = switchdev_port_fdb_dump_cb,
  628. },
  629. .skb = skb,
  630. .cb = cb,
  631. .filter_dev = filter_dev,
  632. .idx = idx,
  633. };
  634. int err;
  635. err = switchdev_port_obj_dump(dev, &dump.obj);
  636. if (err)
  637. return err;
  638. return dump.idx;
  639. }
  640. EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
  641. static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
  642. {
  643. const struct switchdev_ops *ops = dev->switchdev_ops;
  644. struct net_device *lower_dev;
  645. struct net_device *port_dev;
  646. struct list_head *iter;
  647. /* Recusively search down until we find a sw port dev.
  648. * (A sw port dev supports switchdev_port_attr_get).
  649. */
  650. if (ops && ops->switchdev_port_attr_get)
  651. return dev;
  652. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  653. port_dev = switchdev_get_lowest_dev(lower_dev);
  654. if (port_dev)
  655. return port_dev;
  656. }
  657. return NULL;
  658. }
  659. static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
  660. {
  661. struct switchdev_attr attr = {
  662. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  663. };
  664. struct switchdev_attr prev_attr;
  665. struct net_device *dev = NULL;
  666. int nhsel;
  667. /* For this route, all nexthop devs must be on the same switch. */
  668. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  669. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  670. if (!nh->nh_dev)
  671. return NULL;
  672. dev = switchdev_get_lowest_dev(nh->nh_dev);
  673. if (!dev)
  674. return NULL;
  675. if (switchdev_port_attr_get(dev, &attr))
  676. return NULL;
  677. if (nhsel > 0) {
  678. if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len)
  679. return NULL;
  680. if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id,
  681. attr.u.ppid.id_len))
  682. return NULL;
  683. }
  684. prev_attr = attr;
  685. }
  686. return dev;
  687. }
  688. /**
  689. * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
  690. *
  691. * @dst: route's IPv4 destination address
  692. * @dst_len: destination address length (prefix length)
  693. * @fi: route FIB info structure
  694. * @tos: route TOS
  695. * @type: route type
  696. * @nlflags: netlink flags passed in (NLM_F_*)
  697. * @tb_id: route table ID
  698. *
  699. * Add/modify switch IPv4 route entry.
  700. */
  701. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  702. u8 tos, u8 type, u32 nlflags, u32 tb_id)
  703. {
  704. struct switchdev_obj fib_obj = {
  705. .id = SWITCHDEV_OBJ_IPV4_FIB,
  706. .u.ipv4_fib = {
  707. .dst = dst,
  708. .dst_len = dst_len,
  709. .fi = fi,
  710. .tos = tos,
  711. .type = type,
  712. .nlflags = nlflags,
  713. .tb_id = tb_id,
  714. },
  715. };
  716. struct net_device *dev;
  717. int err = 0;
  718. /* Don't offload route if using custom ip rules or if
  719. * IPv4 FIB offloading has been disabled completely.
  720. */
  721. #ifdef CONFIG_IP_MULTIPLE_TABLES
  722. if (fi->fib_net->ipv4.fib_has_custom_rules)
  723. return 0;
  724. #endif
  725. if (fi->fib_net->ipv4.fib_offload_disabled)
  726. return 0;
  727. dev = switchdev_get_dev_by_nhs(fi);
  728. if (!dev)
  729. return 0;
  730. err = switchdev_port_obj_add(dev, &fib_obj);
  731. if (!err)
  732. fi->fib_flags |= RTNH_F_OFFLOAD;
  733. return err == -EOPNOTSUPP ? 0 : err;
  734. }
  735. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
  736. /**
  737. * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
  738. *
  739. * @dst: route's IPv4 destination address
  740. * @dst_len: destination address length (prefix length)
  741. * @fi: route FIB info structure
  742. * @tos: route TOS
  743. * @type: route type
  744. * @tb_id: route table ID
  745. *
  746. * Delete IPv4 route entry from switch device.
  747. */
  748. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  749. u8 tos, u8 type, u32 tb_id)
  750. {
  751. struct switchdev_obj fib_obj = {
  752. .id = SWITCHDEV_OBJ_IPV4_FIB,
  753. .u.ipv4_fib = {
  754. .dst = dst,
  755. .dst_len = dst_len,
  756. .fi = fi,
  757. .tos = tos,
  758. .type = type,
  759. .nlflags = 0,
  760. .tb_id = tb_id,
  761. },
  762. };
  763. struct net_device *dev;
  764. int err = 0;
  765. if (!(fi->fib_flags & RTNH_F_OFFLOAD))
  766. return 0;
  767. dev = switchdev_get_dev_by_nhs(fi);
  768. if (!dev)
  769. return 0;
  770. err = switchdev_port_obj_del(dev, &fib_obj);
  771. if (!err)
  772. fi->fib_flags &= ~RTNH_F_OFFLOAD;
  773. return err == -EOPNOTSUPP ? 0 : err;
  774. }
  775. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
  776. /**
  777. * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
  778. *
  779. * @fi: route FIB info structure
  780. */
  781. void switchdev_fib_ipv4_abort(struct fib_info *fi)
  782. {
  783. /* There was a problem installing this route to the offload
  784. * device. For now, until we come up with more refined
  785. * policy handling, abruptly end IPv4 fib offloading for
  786. * for entire net by flushing offload device(s) of all
  787. * IPv4 routes, and mark IPv4 fib offloading broken from
  788. * this point forward.
  789. */
  790. fib_flush_external(fi->fib_net);
  791. fi->fib_net->ipv4.fib_offload_disabled = true;
  792. }
  793. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);