switchdev.c 28 KB

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