switchdev.c 25 KB

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