switchdev.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. * net/switchdev/switchdev.c - Switch device API
  3. * Copyright (c) 2014-2015 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/etherdevice.h>
  18. #include <linux/if_bridge.h>
  19. #include <linux/list.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/rtnetlink.h>
  23. #include <net/switchdev.h>
  24. /**
  25. * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
  26. *
  27. * @trans: transaction
  28. * @data: pointer to data being queued
  29. * @destructor: data destructor
  30. * @tritem: transaction item being queued
  31. *
  32. * Enqeueue data item to transaction queue. tritem is typically placed in
  33. * cointainter pointed at by data pointer. Destructor is called on
  34. * transaction abort and after successful commit phase in case
  35. * the caller did not dequeue the item before.
  36. */
  37. void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
  38. void *data, void (*destructor)(void const *),
  39. struct switchdev_trans_item *tritem)
  40. {
  41. tritem->data = data;
  42. tritem->destructor = destructor;
  43. list_add_tail(&tritem->list, &trans->item_list);
  44. }
  45. EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue);
  46. static struct switchdev_trans_item *
  47. __switchdev_trans_item_dequeue(struct switchdev_trans *trans)
  48. {
  49. struct switchdev_trans_item *tritem;
  50. if (list_empty(&trans->item_list))
  51. return NULL;
  52. tritem = list_first_entry(&trans->item_list,
  53. struct switchdev_trans_item, list);
  54. list_del(&tritem->list);
  55. return tritem;
  56. }
  57. /**
  58. * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
  59. *
  60. * @trans: transaction
  61. */
  62. void *switchdev_trans_item_dequeue(struct switchdev_trans *trans)
  63. {
  64. struct switchdev_trans_item *tritem;
  65. tritem = __switchdev_trans_item_dequeue(trans);
  66. BUG_ON(!tritem);
  67. return tritem->data;
  68. }
  69. EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue);
  70. static void switchdev_trans_init(struct switchdev_trans *trans)
  71. {
  72. INIT_LIST_HEAD(&trans->item_list);
  73. }
  74. static void switchdev_trans_items_destroy(struct switchdev_trans *trans)
  75. {
  76. struct switchdev_trans_item *tritem;
  77. while ((tritem = __switchdev_trans_item_dequeue(trans)))
  78. tritem->destructor(tritem->data);
  79. }
  80. static void switchdev_trans_items_warn_destroy(struct net_device *dev,
  81. struct switchdev_trans *trans)
  82. {
  83. WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n",
  84. dev->name);
  85. switchdev_trans_items_destroy(trans);
  86. }
  87. static LIST_HEAD(deferred);
  88. static DEFINE_SPINLOCK(deferred_lock);
  89. typedef void switchdev_deferred_func_t(struct net_device *dev,
  90. const void *data);
  91. struct switchdev_deferred_item {
  92. struct list_head list;
  93. struct net_device *dev;
  94. switchdev_deferred_func_t *func;
  95. unsigned long data[0];
  96. };
  97. static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
  98. {
  99. struct switchdev_deferred_item *dfitem;
  100. spin_lock_bh(&deferred_lock);
  101. if (list_empty(&deferred)) {
  102. dfitem = NULL;
  103. goto unlock;
  104. }
  105. dfitem = list_first_entry(&deferred,
  106. struct switchdev_deferred_item, list);
  107. list_del(&dfitem->list);
  108. unlock:
  109. spin_unlock_bh(&deferred_lock);
  110. return dfitem;
  111. }
  112. /**
  113. * switchdev_deferred_process - Process ops in deferred queue
  114. *
  115. * Called to flush the ops currently queued in deferred ops queue.
  116. * rtnl_lock must be held.
  117. */
  118. void switchdev_deferred_process(void)
  119. {
  120. struct switchdev_deferred_item *dfitem;
  121. ASSERT_RTNL();
  122. while ((dfitem = switchdev_deferred_dequeue())) {
  123. dfitem->func(dfitem->dev, dfitem->data);
  124. dev_put(dfitem->dev);
  125. kfree(dfitem);
  126. }
  127. }
  128. EXPORT_SYMBOL_GPL(switchdev_deferred_process);
  129. static void switchdev_deferred_process_work(struct work_struct *work)
  130. {
  131. rtnl_lock();
  132. switchdev_deferred_process();
  133. rtnl_unlock();
  134. }
  135. static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
  136. static int switchdev_deferred_enqueue(struct net_device *dev,
  137. const void *data, size_t data_len,
  138. switchdev_deferred_func_t *func)
  139. {
  140. struct switchdev_deferred_item *dfitem;
  141. dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
  142. if (!dfitem)
  143. return -ENOMEM;
  144. dfitem->dev = dev;
  145. dfitem->func = func;
  146. memcpy(dfitem->data, data, data_len);
  147. dev_hold(dev);
  148. spin_lock_bh(&deferred_lock);
  149. list_add_tail(&dfitem->list, &deferred);
  150. spin_unlock_bh(&deferred_lock);
  151. schedule_work(&deferred_process_work);
  152. return 0;
  153. }
  154. /**
  155. * switchdev_port_attr_get - Get port attribute
  156. *
  157. * @dev: port device
  158. * @attr: attribute to get
  159. */
  160. int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
  161. {
  162. const struct switchdev_ops *ops = dev->switchdev_ops;
  163. struct net_device *lower_dev;
  164. struct list_head *iter;
  165. struct switchdev_attr first = {
  166. .id = SWITCHDEV_ATTR_ID_UNDEFINED
  167. };
  168. int err = -EOPNOTSUPP;
  169. if (ops && ops->switchdev_port_attr_get)
  170. return ops->switchdev_port_attr_get(dev, attr);
  171. if (attr->flags & SWITCHDEV_F_NO_RECURSE)
  172. return err;
  173. /* Switch device port(s) may be stacked under
  174. * bond/team/vlan dev, so recurse down to get attr on
  175. * each port. Return -ENODATA if attr values don't
  176. * compare across ports.
  177. */
  178. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  179. err = switchdev_port_attr_get(lower_dev, attr);
  180. if (err)
  181. break;
  182. if (first.id == SWITCHDEV_ATTR_ID_UNDEFINED)
  183. first = *attr;
  184. else if (memcmp(&first, attr, sizeof(*attr)))
  185. return -ENODATA;
  186. }
  187. return err;
  188. }
  189. EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
  190. static int __switchdev_port_attr_set(struct net_device *dev,
  191. const struct switchdev_attr *attr,
  192. struct switchdev_trans *trans)
  193. {
  194. const struct switchdev_ops *ops = dev->switchdev_ops;
  195. struct net_device *lower_dev;
  196. struct list_head *iter;
  197. int err = -EOPNOTSUPP;
  198. if (ops && ops->switchdev_port_attr_set) {
  199. err = ops->switchdev_port_attr_set(dev, attr, trans);
  200. goto done;
  201. }
  202. if (attr->flags & SWITCHDEV_F_NO_RECURSE)
  203. goto done;
  204. /* Switch device port(s) may be stacked under
  205. * bond/team/vlan dev, so recurse down to set attr on
  206. * each port.
  207. */
  208. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  209. err = __switchdev_port_attr_set(lower_dev, attr, trans);
  210. if (err)
  211. break;
  212. }
  213. done:
  214. if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
  215. err = 0;
  216. return err;
  217. }
  218. static int switchdev_port_attr_set_now(struct net_device *dev,
  219. const struct switchdev_attr *attr)
  220. {
  221. struct switchdev_trans trans;
  222. int err;
  223. switchdev_trans_init(&trans);
  224. /* Phase I: prepare for attr set. Driver/device should fail
  225. * here if there are going to be issues in the commit phase,
  226. * such as lack of resources or support. The driver/device
  227. * should reserve resources needed for the commit phase here,
  228. * but should not commit the attr.
  229. */
  230. trans.ph_prepare = true;
  231. err = __switchdev_port_attr_set(dev, attr, &trans);
  232. if (err) {
  233. /* Prepare phase failed: abort the transaction. Any
  234. * resources reserved in the prepare phase are
  235. * released.
  236. */
  237. if (err != -EOPNOTSUPP)
  238. switchdev_trans_items_destroy(&trans);
  239. return err;
  240. }
  241. /* Phase II: commit attr set. This cannot fail as a fault
  242. * of driver/device. If it does, it's a bug in the driver/device
  243. * because the driver said everythings was OK in phase I.
  244. */
  245. trans.ph_prepare = false;
  246. err = __switchdev_port_attr_set(dev, attr, &trans);
  247. WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
  248. dev->name, attr->id);
  249. switchdev_trans_items_warn_destroy(dev, &trans);
  250. return err;
  251. }
  252. static void switchdev_port_attr_set_deferred(struct net_device *dev,
  253. const void *data)
  254. {
  255. const struct switchdev_attr *attr = data;
  256. int err;
  257. err = switchdev_port_attr_set_now(dev, attr);
  258. if (err && err != -EOPNOTSUPP)
  259. netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
  260. err, attr->id);
  261. if (attr->complete)
  262. attr->complete(dev, err, attr->complete_priv);
  263. }
  264. static int switchdev_port_attr_set_defer(struct net_device *dev,
  265. const struct switchdev_attr *attr)
  266. {
  267. return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
  268. switchdev_port_attr_set_deferred);
  269. }
  270. /**
  271. * switchdev_port_attr_set - Set port attribute
  272. *
  273. * @dev: port device
  274. * @attr: attribute to set
  275. *
  276. * Use a 2-phase prepare-commit transaction model to ensure
  277. * system is not left in a partially updated state due to
  278. * failure from driver/device.
  279. *
  280. * rtnl_lock must be held and must not be in atomic section,
  281. * in case SWITCHDEV_F_DEFER flag is not set.
  282. */
  283. int switchdev_port_attr_set(struct net_device *dev,
  284. const struct switchdev_attr *attr)
  285. {
  286. if (attr->flags & SWITCHDEV_F_DEFER)
  287. return switchdev_port_attr_set_defer(dev, attr);
  288. ASSERT_RTNL();
  289. return switchdev_port_attr_set_now(dev, attr);
  290. }
  291. EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
  292. static size_t switchdev_obj_size(const struct switchdev_obj *obj)
  293. {
  294. switch (obj->id) {
  295. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  296. return sizeof(struct switchdev_obj_port_vlan);
  297. case SWITCHDEV_OBJ_ID_PORT_FDB:
  298. return sizeof(struct switchdev_obj_port_fdb);
  299. case SWITCHDEV_OBJ_ID_PORT_MDB:
  300. return sizeof(struct switchdev_obj_port_mdb);
  301. default:
  302. BUG();
  303. }
  304. return 0;
  305. }
  306. static int __switchdev_port_obj_add(struct net_device *dev,
  307. const struct switchdev_obj *obj,
  308. struct switchdev_trans *trans)
  309. {
  310. const struct switchdev_ops *ops = dev->switchdev_ops;
  311. struct net_device *lower_dev;
  312. struct list_head *iter;
  313. int err = -EOPNOTSUPP;
  314. if (ops && ops->switchdev_port_obj_add)
  315. return ops->switchdev_port_obj_add(dev, obj, trans);
  316. /* Switch device port(s) may be stacked under
  317. * bond/team/vlan dev, so recurse down to add object on
  318. * each port.
  319. */
  320. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  321. err = __switchdev_port_obj_add(lower_dev, obj, trans);
  322. if (err)
  323. break;
  324. }
  325. return err;
  326. }
  327. static int switchdev_port_obj_add_now(struct net_device *dev,
  328. const struct switchdev_obj *obj)
  329. {
  330. struct switchdev_trans trans;
  331. int err;
  332. ASSERT_RTNL();
  333. switchdev_trans_init(&trans);
  334. /* Phase I: prepare for obj add. Driver/device should fail
  335. * here if there are going to be issues in the commit phase,
  336. * such as lack of resources or support. The driver/device
  337. * should reserve resources needed for the commit phase here,
  338. * but should not commit the obj.
  339. */
  340. trans.ph_prepare = true;
  341. err = __switchdev_port_obj_add(dev, obj, &trans);
  342. if (err) {
  343. /* Prepare phase failed: abort the transaction. Any
  344. * resources reserved in the prepare phase are
  345. * released.
  346. */
  347. if (err != -EOPNOTSUPP)
  348. switchdev_trans_items_destroy(&trans);
  349. return err;
  350. }
  351. /* Phase II: commit obj add. This cannot fail as a fault
  352. * of driver/device. If it does, it's a bug in the driver/device
  353. * because the driver said everythings was OK in phase I.
  354. */
  355. trans.ph_prepare = false;
  356. err = __switchdev_port_obj_add(dev, obj, &trans);
  357. WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
  358. switchdev_trans_items_warn_destroy(dev, &trans);
  359. return err;
  360. }
  361. static void switchdev_port_obj_add_deferred(struct net_device *dev,
  362. const void *data)
  363. {
  364. const struct switchdev_obj *obj = data;
  365. int err;
  366. err = switchdev_port_obj_add_now(dev, obj);
  367. if (err && err != -EOPNOTSUPP)
  368. netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
  369. err, obj->id);
  370. if (obj->complete)
  371. obj->complete(dev, err, obj->complete_priv);
  372. }
  373. static int switchdev_port_obj_add_defer(struct net_device *dev,
  374. const struct switchdev_obj *obj)
  375. {
  376. return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
  377. switchdev_port_obj_add_deferred);
  378. }
  379. /**
  380. * switchdev_port_obj_add - Add port object
  381. *
  382. * @dev: port device
  383. * @id: object ID
  384. * @obj: object to add
  385. *
  386. * Use a 2-phase prepare-commit transaction model to ensure
  387. * system is not left in a partially updated state due to
  388. * failure from driver/device.
  389. *
  390. * rtnl_lock must be held and must not be in atomic section,
  391. * in case SWITCHDEV_F_DEFER flag is not set.
  392. */
  393. int switchdev_port_obj_add(struct net_device *dev,
  394. const struct switchdev_obj *obj)
  395. {
  396. if (obj->flags & SWITCHDEV_F_DEFER)
  397. return switchdev_port_obj_add_defer(dev, obj);
  398. ASSERT_RTNL();
  399. return switchdev_port_obj_add_now(dev, obj);
  400. }
  401. EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
  402. static int switchdev_port_obj_del_now(struct net_device *dev,
  403. const struct switchdev_obj *obj)
  404. {
  405. const struct switchdev_ops *ops = dev->switchdev_ops;
  406. struct net_device *lower_dev;
  407. struct list_head *iter;
  408. int err = -EOPNOTSUPP;
  409. if (ops && ops->switchdev_port_obj_del)
  410. return ops->switchdev_port_obj_del(dev, obj);
  411. /* Switch device port(s) may be stacked under
  412. * bond/team/vlan dev, so recurse down to delete object on
  413. * each port.
  414. */
  415. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  416. err = switchdev_port_obj_del_now(lower_dev, obj);
  417. if (err)
  418. break;
  419. }
  420. return err;
  421. }
  422. static void switchdev_port_obj_del_deferred(struct net_device *dev,
  423. const void *data)
  424. {
  425. const struct switchdev_obj *obj = data;
  426. int err;
  427. err = switchdev_port_obj_del_now(dev, obj);
  428. if (err && err != -EOPNOTSUPP)
  429. netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
  430. err, obj->id);
  431. if (obj->complete)
  432. obj->complete(dev, err, obj->complete_priv);
  433. }
  434. static int switchdev_port_obj_del_defer(struct net_device *dev,
  435. const struct switchdev_obj *obj)
  436. {
  437. return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
  438. switchdev_port_obj_del_deferred);
  439. }
  440. /**
  441. * switchdev_port_obj_del - Delete port object
  442. *
  443. * @dev: port device
  444. * @id: object ID
  445. * @obj: object to delete
  446. *
  447. * rtnl_lock must be held and must not be in atomic section,
  448. * in case SWITCHDEV_F_DEFER flag is not set.
  449. */
  450. int switchdev_port_obj_del(struct net_device *dev,
  451. const struct switchdev_obj *obj)
  452. {
  453. if (obj->flags & SWITCHDEV_F_DEFER)
  454. return switchdev_port_obj_del_defer(dev, obj);
  455. ASSERT_RTNL();
  456. return switchdev_port_obj_del_now(dev, obj);
  457. }
  458. EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
  459. /**
  460. * switchdev_port_obj_dump - Dump port objects
  461. *
  462. * @dev: port device
  463. * @id: object ID
  464. * @obj: object to dump
  465. * @cb: function to call with a filled object
  466. *
  467. * rtnl_lock must be held.
  468. */
  469. int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
  470. switchdev_obj_dump_cb_t *cb)
  471. {
  472. const struct switchdev_ops *ops = dev->switchdev_ops;
  473. struct net_device *lower_dev;
  474. struct list_head *iter;
  475. int err = -EOPNOTSUPP;
  476. ASSERT_RTNL();
  477. if (ops && ops->switchdev_port_obj_dump)
  478. return ops->switchdev_port_obj_dump(dev, obj, cb);
  479. /* Switch device port(s) may be stacked under
  480. * bond/team/vlan dev, so recurse down to dump objects on
  481. * first port at bottom of stack.
  482. */
  483. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  484. err = switchdev_port_obj_dump(lower_dev, obj, cb);
  485. break;
  486. }
  487. return err;
  488. }
  489. EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
  490. static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
  491. /**
  492. * register_switchdev_notifier - Register notifier
  493. * @nb: notifier_block
  494. *
  495. * Register switch device notifier. This should be used by code
  496. * which needs to monitor events happening in particular device.
  497. * Return values are same as for atomic_notifier_chain_register().
  498. */
  499. int register_switchdev_notifier(struct notifier_block *nb)
  500. {
  501. int err;
  502. rtnl_lock();
  503. err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
  504. rtnl_unlock();
  505. return err;
  506. }
  507. EXPORT_SYMBOL_GPL(register_switchdev_notifier);
  508. /**
  509. * unregister_switchdev_notifier - Unregister notifier
  510. * @nb: notifier_block
  511. *
  512. * Unregister switch device notifier.
  513. * Return values are same as for atomic_notifier_chain_unregister().
  514. */
  515. int unregister_switchdev_notifier(struct notifier_block *nb)
  516. {
  517. int err;
  518. rtnl_lock();
  519. err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
  520. rtnl_unlock();
  521. return err;
  522. }
  523. EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
  524. /**
  525. * call_switchdev_notifiers - Call notifiers
  526. * @val: value passed unmodified to notifier function
  527. * @dev: port device
  528. * @info: notifier information data
  529. *
  530. * Call all network notifier blocks. This should be called by driver
  531. * when it needs to propagate hardware event.
  532. * Return values are same as for atomic_notifier_call_chain().
  533. * rtnl_lock must be held.
  534. */
  535. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  536. struct switchdev_notifier_info *info)
  537. {
  538. ASSERT_RTNL();
  539. info->dev = dev;
  540. return raw_notifier_call_chain(&switchdev_notif_chain, val, info);
  541. }
  542. EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
  543. struct switchdev_vlan_dump {
  544. struct switchdev_obj_port_vlan vlan;
  545. struct sk_buff *skb;
  546. u32 filter_mask;
  547. u16 flags;
  548. u16 begin;
  549. u16 end;
  550. };
  551. static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
  552. {
  553. struct bridge_vlan_info vinfo;
  554. vinfo.flags = dump->flags;
  555. if (dump->begin == 0 && dump->end == 0) {
  556. return 0;
  557. } else if (dump->begin == dump->end) {
  558. vinfo.vid = dump->begin;
  559. if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
  560. sizeof(vinfo), &vinfo))
  561. return -EMSGSIZE;
  562. } else {
  563. vinfo.vid = dump->begin;
  564. vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
  565. if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
  566. sizeof(vinfo), &vinfo))
  567. return -EMSGSIZE;
  568. vinfo.vid = dump->end;
  569. vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
  570. vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
  571. if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
  572. sizeof(vinfo), &vinfo))
  573. return -EMSGSIZE;
  574. }
  575. return 0;
  576. }
  577. static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
  578. {
  579. struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
  580. struct switchdev_vlan_dump *dump =
  581. container_of(vlan, struct switchdev_vlan_dump, vlan);
  582. int err = 0;
  583. if (vlan->vid_begin > vlan->vid_end)
  584. return -EINVAL;
  585. if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
  586. dump->flags = vlan->flags;
  587. for (dump->begin = dump->end = vlan->vid_begin;
  588. dump->begin <= vlan->vid_end;
  589. dump->begin++, dump->end++) {
  590. err = switchdev_port_vlan_dump_put(dump);
  591. if (err)
  592. return err;
  593. }
  594. } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
  595. if (dump->begin > vlan->vid_begin &&
  596. dump->begin >= vlan->vid_end) {
  597. if ((dump->begin - 1) == vlan->vid_end &&
  598. dump->flags == vlan->flags) {
  599. /* prepend */
  600. dump->begin = vlan->vid_begin;
  601. } else {
  602. err = switchdev_port_vlan_dump_put(dump);
  603. dump->flags = vlan->flags;
  604. dump->begin = vlan->vid_begin;
  605. dump->end = vlan->vid_end;
  606. }
  607. } else if (dump->end <= vlan->vid_begin &&
  608. dump->end < vlan->vid_end) {
  609. if ((dump->end + 1) == vlan->vid_begin &&
  610. dump->flags == vlan->flags) {
  611. /* append */
  612. dump->end = vlan->vid_end;
  613. } else {
  614. err = switchdev_port_vlan_dump_put(dump);
  615. dump->flags = vlan->flags;
  616. dump->begin = vlan->vid_begin;
  617. dump->end = vlan->vid_end;
  618. }
  619. } else {
  620. err = -EINVAL;
  621. }
  622. }
  623. return err;
  624. }
  625. static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
  626. u32 filter_mask)
  627. {
  628. struct switchdev_vlan_dump dump = {
  629. .vlan.obj.orig_dev = dev,
  630. .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  631. .skb = skb,
  632. .filter_mask = filter_mask,
  633. };
  634. int err = 0;
  635. if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
  636. (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
  637. err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
  638. switchdev_port_vlan_dump_cb);
  639. if (err)
  640. goto err_out;
  641. if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
  642. /* last one */
  643. err = switchdev_port_vlan_dump_put(&dump);
  644. }
  645. err_out:
  646. return err == -EOPNOTSUPP ? 0 : err;
  647. }
  648. /**
  649. * switchdev_port_bridge_getlink - Get bridge port attributes
  650. *
  651. * @dev: port device
  652. *
  653. * Called for SELF on rtnl_bridge_getlink to get bridge port
  654. * attributes.
  655. */
  656. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  657. struct net_device *dev, u32 filter_mask,
  658. int nlflags)
  659. {
  660. struct switchdev_attr attr = {
  661. .orig_dev = dev,
  662. .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
  663. };
  664. u16 mode = BRIDGE_MODE_UNDEF;
  665. u32 mask = BR_LEARNING | BR_LEARNING_SYNC | BR_FLOOD;
  666. int err;
  667. if (!netif_is_bridge_port(dev))
  668. return -EOPNOTSUPP;
  669. err = switchdev_port_attr_get(dev, &attr);
  670. if (err && err != -EOPNOTSUPP)
  671. return err;
  672. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
  673. attr.u.brport_flags, mask, nlflags,
  674. filter_mask, switchdev_port_vlan_fill);
  675. }
  676. EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
  677. static int switchdev_port_br_setflag(struct net_device *dev,
  678. struct nlattr *nlattr,
  679. unsigned long brport_flag)
  680. {
  681. struct switchdev_attr attr = {
  682. .orig_dev = dev,
  683. .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
  684. };
  685. u8 flag = nla_get_u8(nlattr);
  686. int err;
  687. err = switchdev_port_attr_get(dev, &attr);
  688. if (err)
  689. return err;
  690. if (flag)
  691. attr.u.brport_flags |= brport_flag;
  692. else
  693. attr.u.brport_flags &= ~brport_flag;
  694. return switchdev_port_attr_set(dev, &attr);
  695. }
  696. static const struct nla_policy
  697. switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
  698. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  699. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  700. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  701. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  702. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  703. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  704. [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
  705. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  706. [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
  707. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  708. };
  709. static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
  710. struct nlattr *protinfo)
  711. {
  712. struct nlattr *attr;
  713. int rem;
  714. int err;
  715. err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
  716. switchdev_port_bridge_policy);
  717. if (err)
  718. return err;
  719. nla_for_each_nested(attr, protinfo, rem) {
  720. switch (nla_type(attr)) {
  721. case IFLA_BRPORT_LEARNING:
  722. err = switchdev_port_br_setflag(dev, attr,
  723. BR_LEARNING);
  724. break;
  725. case IFLA_BRPORT_LEARNING_SYNC:
  726. err = switchdev_port_br_setflag(dev, attr,
  727. BR_LEARNING_SYNC);
  728. break;
  729. case IFLA_BRPORT_UNICAST_FLOOD:
  730. err = switchdev_port_br_setflag(dev, attr, BR_FLOOD);
  731. break;
  732. default:
  733. err = -EOPNOTSUPP;
  734. break;
  735. }
  736. if (err)
  737. return err;
  738. }
  739. return 0;
  740. }
  741. static int switchdev_port_br_afspec(struct net_device *dev,
  742. struct nlattr *afspec,
  743. int (*f)(struct net_device *dev,
  744. const struct switchdev_obj *obj))
  745. {
  746. struct nlattr *attr;
  747. struct bridge_vlan_info *vinfo;
  748. struct switchdev_obj_port_vlan vlan = {
  749. .obj.orig_dev = dev,
  750. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  751. };
  752. int rem;
  753. int err;
  754. nla_for_each_nested(attr, afspec, rem) {
  755. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  756. continue;
  757. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  758. return -EINVAL;
  759. vinfo = nla_data(attr);
  760. if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
  761. return -EINVAL;
  762. vlan.flags = vinfo->flags;
  763. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  764. if (vlan.vid_begin)
  765. return -EINVAL;
  766. vlan.vid_begin = vinfo->vid;
  767. /* don't allow range of pvids */
  768. if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
  769. return -EINVAL;
  770. } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  771. if (!vlan.vid_begin)
  772. return -EINVAL;
  773. vlan.vid_end = vinfo->vid;
  774. if (vlan.vid_end <= vlan.vid_begin)
  775. return -EINVAL;
  776. err = f(dev, &vlan.obj);
  777. if (err)
  778. return err;
  779. vlan.vid_begin = 0;
  780. } else {
  781. if (vlan.vid_begin)
  782. return -EINVAL;
  783. vlan.vid_begin = vinfo->vid;
  784. vlan.vid_end = vinfo->vid;
  785. err = f(dev, &vlan.obj);
  786. if (err)
  787. return err;
  788. vlan.vid_begin = 0;
  789. }
  790. }
  791. return 0;
  792. }
  793. /**
  794. * switchdev_port_bridge_setlink - Set bridge port attributes
  795. *
  796. * @dev: port device
  797. * @nlh: netlink header
  798. * @flags: netlink flags
  799. *
  800. * Called for SELF on rtnl_bridge_setlink to set bridge port
  801. * attributes.
  802. */
  803. int switchdev_port_bridge_setlink(struct net_device *dev,
  804. struct nlmsghdr *nlh, u16 flags)
  805. {
  806. struct nlattr *protinfo;
  807. struct nlattr *afspec;
  808. int err = 0;
  809. if (!netif_is_bridge_port(dev))
  810. return -EOPNOTSUPP;
  811. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  812. IFLA_PROTINFO);
  813. if (protinfo) {
  814. err = switchdev_port_br_setlink_protinfo(dev, protinfo);
  815. if (err)
  816. return err;
  817. }
  818. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  819. IFLA_AF_SPEC);
  820. if (afspec)
  821. err = switchdev_port_br_afspec(dev, afspec,
  822. switchdev_port_obj_add);
  823. return err;
  824. }
  825. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  826. /**
  827. * switchdev_port_bridge_dellink - Set bridge port attributes
  828. *
  829. * @dev: port device
  830. * @nlh: netlink header
  831. * @flags: netlink flags
  832. *
  833. * Called for SELF on rtnl_bridge_dellink to set bridge port
  834. * attributes.
  835. */
  836. int switchdev_port_bridge_dellink(struct net_device *dev,
  837. struct nlmsghdr *nlh, u16 flags)
  838. {
  839. struct nlattr *afspec;
  840. if (!netif_is_bridge_port(dev))
  841. return -EOPNOTSUPP;
  842. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  843. IFLA_AF_SPEC);
  844. if (afspec)
  845. return switchdev_port_br_afspec(dev, afspec,
  846. switchdev_port_obj_del);
  847. return 0;
  848. }
  849. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  850. /**
  851. * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
  852. *
  853. * @ndmsg: netlink hdr
  854. * @nlattr: netlink attributes
  855. * @dev: port device
  856. * @addr: MAC address to add
  857. * @vid: VLAN to add
  858. *
  859. * Add FDB entry to switch device.
  860. */
  861. int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  862. struct net_device *dev, const unsigned char *addr,
  863. u16 vid, u16 nlm_flags)
  864. {
  865. struct switchdev_obj_port_fdb fdb = {
  866. .obj.orig_dev = dev,
  867. .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
  868. .vid = vid,
  869. };
  870. ether_addr_copy(fdb.addr, addr);
  871. return switchdev_port_obj_add(dev, &fdb.obj);
  872. }
  873. EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
  874. /**
  875. * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
  876. *
  877. * @ndmsg: netlink hdr
  878. * @nlattr: netlink attributes
  879. * @dev: port device
  880. * @addr: MAC address to delete
  881. * @vid: VLAN to delete
  882. *
  883. * Delete FDB entry from switch device.
  884. */
  885. int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  886. struct net_device *dev, const unsigned char *addr,
  887. u16 vid)
  888. {
  889. struct switchdev_obj_port_fdb fdb = {
  890. .obj.orig_dev = dev,
  891. .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
  892. .vid = vid,
  893. };
  894. ether_addr_copy(fdb.addr, addr);
  895. return switchdev_port_obj_del(dev, &fdb.obj);
  896. }
  897. EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
  898. struct switchdev_fdb_dump {
  899. struct switchdev_obj_port_fdb fdb;
  900. struct net_device *dev;
  901. struct sk_buff *skb;
  902. struct netlink_callback *cb;
  903. int idx;
  904. };
  905. static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
  906. {
  907. struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
  908. struct switchdev_fdb_dump *dump =
  909. container_of(fdb, struct switchdev_fdb_dump, fdb);
  910. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  911. u32 seq = dump->cb->nlh->nlmsg_seq;
  912. struct nlmsghdr *nlh;
  913. struct ndmsg *ndm;
  914. if (dump->idx < dump->cb->args[2])
  915. goto skip;
  916. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  917. sizeof(*ndm), NLM_F_MULTI);
  918. if (!nlh)
  919. return -EMSGSIZE;
  920. ndm = nlmsg_data(nlh);
  921. ndm->ndm_family = AF_BRIDGE;
  922. ndm->ndm_pad1 = 0;
  923. ndm->ndm_pad2 = 0;
  924. ndm->ndm_flags = NTF_SELF;
  925. ndm->ndm_type = 0;
  926. ndm->ndm_ifindex = dump->dev->ifindex;
  927. ndm->ndm_state = fdb->ndm_state;
  928. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
  929. goto nla_put_failure;
  930. if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
  931. goto nla_put_failure;
  932. nlmsg_end(dump->skb, nlh);
  933. skip:
  934. dump->idx++;
  935. return 0;
  936. nla_put_failure:
  937. nlmsg_cancel(dump->skb, nlh);
  938. return -EMSGSIZE;
  939. }
  940. /**
  941. * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
  942. *
  943. * @skb: netlink skb
  944. * @cb: netlink callback
  945. * @dev: port device
  946. * @filter_dev: filter device
  947. * @idx:
  948. *
  949. * Dump FDB entries from switch device.
  950. */
  951. int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  952. struct net_device *dev,
  953. struct net_device *filter_dev, int *idx)
  954. {
  955. struct switchdev_fdb_dump dump = {
  956. .fdb.obj.orig_dev = dev,
  957. .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
  958. .dev = dev,
  959. .skb = skb,
  960. .cb = cb,
  961. .idx = *idx,
  962. };
  963. int err;
  964. err = switchdev_port_obj_dump(dev, &dump.fdb.obj,
  965. switchdev_port_fdb_dump_cb);
  966. *idx = dump.idx;
  967. return err;
  968. }
  969. EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
  970. bool switchdev_port_same_parent_id(struct net_device *a,
  971. struct net_device *b)
  972. {
  973. struct switchdev_attr a_attr = {
  974. .orig_dev = a,
  975. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  976. };
  977. struct switchdev_attr b_attr = {
  978. .orig_dev = b,
  979. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  980. };
  981. if (switchdev_port_attr_get(a, &a_attr) ||
  982. switchdev_port_attr_get(b, &b_attr))
  983. return false;
  984. return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
  985. }
  986. EXPORT_SYMBOL_GPL(switchdev_port_same_parent_id);