switchdev.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. int err;
  539. ASSERT_RTNL();
  540. info->dev = dev;
  541. err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
  542. return err;
  543. }
  544. EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
  545. struct switchdev_vlan_dump {
  546. struct switchdev_obj_port_vlan vlan;
  547. struct sk_buff *skb;
  548. u32 filter_mask;
  549. u16 flags;
  550. u16 begin;
  551. u16 end;
  552. };
  553. static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
  554. {
  555. struct bridge_vlan_info vinfo;
  556. vinfo.flags = dump->flags;
  557. if (dump->begin == 0 && dump->end == 0) {
  558. return 0;
  559. } else if (dump->begin == dump->end) {
  560. vinfo.vid = dump->begin;
  561. if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
  562. sizeof(vinfo), &vinfo))
  563. return -EMSGSIZE;
  564. } else {
  565. vinfo.vid = dump->begin;
  566. vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
  567. if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
  568. sizeof(vinfo), &vinfo))
  569. return -EMSGSIZE;
  570. vinfo.vid = dump->end;
  571. vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
  572. vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
  573. if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
  574. sizeof(vinfo), &vinfo))
  575. return -EMSGSIZE;
  576. }
  577. return 0;
  578. }
  579. static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
  580. {
  581. struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
  582. struct switchdev_vlan_dump *dump =
  583. container_of(vlan, struct switchdev_vlan_dump, vlan);
  584. int err = 0;
  585. if (vlan->vid_begin > vlan->vid_end)
  586. return -EINVAL;
  587. if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
  588. dump->flags = vlan->flags;
  589. for (dump->begin = dump->end = vlan->vid_begin;
  590. dump->begin <= vlan->vid_end;
  591. dump->begin++, dump->end++) {
  592. err = switchdev_port_vlan_dump_put(dump);
  593. if (err)
  594. return err;
  595. }
  596. } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
  597. if (dump->begin > vlan->vid_begin &&
  598. dump->begin >= vlan->vid_end) {
  599. if ((dump->begin - 1) == vlan->vid_end &&
  600. dump->flags == vlan->flags) {
  601. /* prepend */
  602. dump->begin = vlan->vid_begin;
  603. } else {
  604. err = switchdev_port_vlan_dump_put(dump);
  605. dump->flags = vlan->flags;
  606. dump->begin = vlan->vid_begin;
  607. dump->end = vlan->vid_end;
  608. }
  609. } else if (dump->end <= vlan->vid_begin &&
  610. dump->end < vlan->vid_end) {
  611. if ((dump->end + 1) == vlan->vid_begin &&
  612. dump->flags == vlan->flags) {
  613. /* append */
  614. dump->end = vlan->vid_end;
  615. } else {
  616. err = switchdev_port_vlan_dump_put(dump);
  617. dump->flags = vlan->flags;
  618. dump->begin = vlan->vid_begin;
  619. dump->end = vlan->vid_end;
  620. }
  621. } else {
  622. err = -EINVAL;
  623. }
  624. }
  625. return err;
  626. }
  627. static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
  628. u32 filter_mask)
  629. {
  630. struct switchdev_vlan_dump dump = {
  631. .vlan.obj.orig_dev = dev,
  632. .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  633. .skb = skb,
  634. .filter_mask = filter_mask,
  635. };
  636. int err = 0;
  637. if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
  638. (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
  639. err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
  640. switchdev_port_vlan_dump_cb);
  641. if (err)
  642. goto err_out;
  643. if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
  644. /* last one */
  645. err = switchdev_port_vlan_dump_put(&dump);
  646. }
  647. err_out:
  648. return err == -EOPNOTSUPP ? 0 : err;
  649. }
  650. /**
  651. * switchdev_port_bridge_getlink - Get bridge port attributes
  652. *
  653. * @dev: port device
  654. *
  655. * Called for SELF on rtnl_bridge_getlink to get bridge port
  656. * attributes.
  657. */
  658. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  659. struct net_device *dev, u32 filter_mask,
  660. int nlflags)
  661. {
  662. struct switchdev_attr attr = {
  663. .orig_dev = dev,
  664. .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
  665. };
  666. u16 mode = BRIDGE_MODE_UNDEF;
  667. u32 mask = BR_LEARNING | BR_LEARNING_SYNC | BR_FLOOD;
  668. int err;
  669. if (!netif_is_bridge_port(dev))
  670. return -EOPNOTSUPP;
  671. err = switchdev_port_attr_get(dev, &attr);
  672. if (err && err != -EOPNOTSUPP)
  673. return err;
  674. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
  675. attr.u.brport_flags, mask, nlflags,
  676. filter_mask, switchdev_port_vlan_fill);
  677. }
  678. EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
  679. static int switchdev_port_br_setflag(struct net_device *dev,
  680. struct nlattr *nlattr,
  681. unsigned long brport_flag)
  682. {
  683. struct switchdev_attr attr = {
  684. .orig_dev = dev,
  685. .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
  686. };
  687. u8 flag = nla_get_u8(nlattr);
  688. int err;
  689. err = switchdev_port_attr_get(dev, &attr);
  690. if (err)
  691. return err;
  692. if (flag)
  693. attr.u.brport_flags |= brport_flag;
  694. else
  695. attr.u.brport_flags &= ~brport_flag;
  696. return switchdev_port_attr_set(dev, &attr);
  697. }
  698. static const struct nla_policy
  699. switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
  700. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  701. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  702. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  703. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  704. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  705. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  706. [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
  707. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  708. [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
  709. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  710. };
  711. static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
  712. struct nlattr *protinfo)
  713. {
  714. struct nlattr *attr;
  715. int rem;
  716. int err;
  717. err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
  718. switchdev_port_bridge_policy);
  719. if (err)
  720. return err;
  721. nla_for_each_nested(attr, protinfo, rem) {
  722. switch (nla_type(attr)) {
  723. case IFLA_BRPORT_LEARNING:
  724. err = switchdev_port_br_setflag(dev, attr,
  725. BR_LEARNING);
  726. break;
  727. case IFLA_BRPORT_LEARNING_SYNC:
  728. err = switchdev_port_br_setflag(dev, attr,
  729. BR_LEARNING_SYNC);
  730. break;
  731. case IFLA_BRPORT_UNICAST_FLOOD:
  732. err = switchdev_port_br_setflag(dev, attr, BR_FLOOD);
  733. break;
  734. default:
  735. err = -EOPNOTSUPP;
  736. break;
  737. }
  738. if (err)
  739. return err;
  740. }
  741. return 0;
  742. }
  743. static int switchdev_port_br_afspec(struct net_device *dev,
  744. struct nlattr *afspec,
  745. int (*f)(struct net_device *dev,
  746. const struct switchdev_obj *obj))
  747. {
  748. struct nlattr *attr;
  749. struct bridge_vlan_info *vinfo;
  750. struct switchdev_obj_port_vlan vlan = {
  751. .obj.orig_dev = dev,
  752. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  753. };
  754. int rem;
  755. int err;
  756. nla_for_each_nested(attr, afspec, rem) {
  757. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  758. continue;
  759. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  760. return -EINVAL;
  761. vinfo = nla_data(attr);
  762. if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
  763. return -EINVAL;
  764. vlan.flags = vinfo->flags;
  765. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  766. if (vlan.vid_begin)
  767. return -EINVAL;
  768. vlan.vid_begin = vinfo->vid;
  769. /* don't allow range of pvids */
  770. if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
  771. return -EINVAL;
  772. } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  773. if (!vlan.vid_begin)
  774. return -EINVAL;
  775. vlan.vid_end = vinfo->vid;
  776. if (vlan.vid_end <= vlan.vid_begin)
  777. return -EINVAL;
  778. err = f(dev, &vlan.obj);
  779. if (err)
  780. return err;
  781. vlan.vid_begin = 0;
  782. } else {
  783. if (vlan.vid_begin)
  784. return -EINVAL;
  785. vlan.vid_begin = vinfo->vid;
  786. vlan.vid_end = vinfo->vid;
  787. err = f(dev, &vlan.obj);
  788. if (err)
  789. return err;
  790. vlan.vid_begin = 0;
  791. }
  792. }
  793. return 0;
  794. }
  795. /**
  796. * switchdev_port_bridge_setlink - Set bridge port attributes
  797. *
  798. * @dev: port device
  799. * @nlh: netlink header
  800. * @flags: netlink flags
  801. *
  802. * Called for SELF on rtnl_bridge_setlink to set bridge port
  803. * attributes.
  804. */
  805. int switchdev_port_bridge_setlink(struct net_device *dev,
  806. struct nlmsghdr *nlh, u16 flags)
  807. {
  808. struct nlattr *protinfo;
  809. struct nlattr *afspec;
  810. int err = 0;
  811. if (!netif_is_bridge_port(dev))
  812. return -EOPNOTSUPP;
  813. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  814. IFLA_PROTINFO);
  815. if (protinfo) {
  816. err = switchdev_port_br_setlink_protinfo(dev, protinfo);
  817. if (err)
  818. return err;
  819. }
  820. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  821. IFLA_AF_SPEC);
  822. if (afspec)
  823. err = switchdev_port_br_afspec(dev, afspec,
  824. switchdev_port_obj_add);
  825. return err;
  826. }
  827. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  828. /**
  829. * switchdev_port_bridge_dellink - Set bridge port attributes
  830. *
  831. * @dev: port device
  832. * @nlh: netlink header
  833. * @flags: netlink flags
  834. *
  835. * Called for SELF on rtnl_bridge_dellink to set bridge port
  836. * attributes.
  837. */
  838. int switchdev_port_bridge_dellink(struct net_device *dev,
  839. struct nlmsghdr *nlh, u16 flags)
  840. {
  841. struct nlattr *afspec;
  842. if (!netif_is_bridge_port(dev))
  843. return -EOPNOTSUPP;
  844. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
  845. IFLA_AF_SPEC);
  846. if (afspec)
  847. return switchdev_port_br_afspec(dev, afspec,
  848. switchdev_port_obj_del);
  849. return 0;
  850. }
  851. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  852. /**
  853. * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
  854. *
  855. * @ndmsg: netlink hdr
  856. * @nlattr: netlink attributes
  857. * @dev: port device
  858. * @addr: MAC address to add
  859. * @vid: VLAN to add
  860. *
  861. * Add FDB entry to switch device.
  862. */
  863. int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  864. struct net_device *dev, const unsigned char *addr,
  865. u16 vid, u16 nlm_flags)
  866. {
  867. struct switchdev_obj_port_fdb fdb = {
  868. .obj.orig_dev = dev,
  869. .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
  870. .vid = vid,
  871. };
  872. ether_addr_copy(fdb.addr, addr);
  873. return switchdev_port_obj_add(dev, &fdb.obj);
  874. }
  875. EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
  876. /**
  877. * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
  878. *
  879. * @ndmsg: netlink hdr
  880. * @nlattr: netlink attributes
  881. * @dev: port device
  882. * @addr: MAC address to delete
  883. * @vid: VLAN to delete
  884. *
  885. * Delete FDB entry from switch device.
  886. */
  887. int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  888. struct net_device *dev, const unsigned char *addr,
  889. u16 vid)
  890. {
  891. struct switchdev_obj_port_fdb fdb = {
  892. .obj.orig_dev = dev,
  893. .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
  894. .vid = vid,
  895. };
  896. ether_addr_copy(fdb.addr, addr);
  897. return switchdev_port_obj_del(dev, &fdb.obj);
  898. }
  899. EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
  900. struct switchdev_fdb_dump {
  901. struct switchdev_obj_port_fdb fdb;
  902. struct net_device *dev;
  903. struct sk_buff *skb;
  904. struct netlink_callback *cb;
  905. int idx;
  906. };
  907. static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
  908. {
  909. struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
  910. struct switchdev_fdb_dump *dump =
  911. container_of(fdb, struct switchdev_fdb_dump, fdb);
  912. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  913. u32 seq = dump->cb->nlh->nlmsg_seq;
  914. struct nlmsghdr *nlh;
  915. struct ndmsg *ndm;
  916. if (dump->idx < dump->cb->args[2])
  917. goto skip;
  918. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  919. sizeof(*ndm), NLM_F_MULTI);
  920. if (!nlh)
  921. return -EMSGSIZE;
  922. ndm = nlmsg_data(nlh);
  923. ndm->ndm_family = AF_BRIDGE;
  924. ndm->ndm_pad1 = 0;
  925. ndm->ndm_pad2 = 0;
  926. ndm->ndm_flags = NTF_SELF;
  927. ndm->ndm_type = 0;
  928. ndm->ndm_ifindex = dump->dev->ifindex;
  929. ndm->ndm_state = fdb->ndm_state;
  930. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
  931. goto nla_put_failure;
  932. if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
  933. goto nla_put_failure;
  934. nlmsg_end(dump->skb, nlh);
  935. skip:
  936. dump->idx++;
  937. return 0;
  938. nla_put_failure:
  939. nlmsg_cancel(dump->skb, nlh);
  940. return -EMSGSIZE;
  941. }
  942. /**
  943. * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
  944. *
  945. * @skb: netlink skb
  946. * @cb: netlink callback
  947. * @dev: port device
  948. * @filter_dev: filter device
  949. * @idx:
  950. *
  951. * Dump FDB entries from switch device.
  952. */
  953. int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  954. struct net_device *dev,
  955. struct net_device *filter_dev, int *idx)
  956. {
  957. struct switchdev_fdb_dump dump = {
  958. .fdb.obj.orig_dev = dev,
  959. .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
  960. .dev = dev,
  961. .skb = skb,
  962. .cb = cb,
  963. .idx = *idx,
  964. };
  965. int err;
  966. err = switchdev_port_obj_dump(dev, &dump.fdb.obj,
  967. switchdev_port_fdb_dump_cb);
  968. *idx = dump.idx;
  969. return err;
  970. }
  971. EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
  972. bool switchdev_port_same_parent_id(struct net_device *a,
  973. struct net_device *b)
  974. {
  975. struct switchdev_attr a_attr = {
  976. .orig_dev = a,
  977. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  978. };
  979. struct switchdev_attr b_attr = {
  980. .orig_dev = b,
  981. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  982. };
  983. if (switchdev_port_attr_get(a, &a_attr) ||
  984. switchdev_port_attr_get(b, &b_attr))
  985. return false;
  986. return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
  987. }
  988. EXPORT_SYMBOL_GPL(switchdev_port_same_parent_id);