br_switchdev.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/list.h>
  4. #include <linux/netdevice.h>
  5. #include <linux/rtnetlink.h>
  6. #include <linux/skbuff.h>
  7. #include <net/switchdev.h>
  8. #include "br_private.h"
  9. static int br_switchdev_mark_get(struct net_bridge *br, struct net_device *dev)
  10. {
  11. struct net_bridge_port *p;
  12. /* dev is yet to be added to the port list. */
  13. list_for_each_entry(p, &br->port_list, list) {
  14. if (switchdev_port_same_parent_id(dev, p->dev))
  15. return p->offload_fwd_mark;
  16. }
  17. return ++br->offload_fwd_mark;
  18. }
  19. int nbp_switchdev_mark_set(struct net_bridge_port *p)
  20. {
  21. struct switchdev_attr attr = {
  22. .orig_dev = p->dev,
  23. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  24. };
  25. int err;
  26. ASSERT_RTNL();
  27. err = switchdev_port_attr_get(p->dev, &attr);
  28. if (err) {
  29. if (err == -EOPNOTSUPP)
  30. return 0;
  31. return err;
  32. }
  33. p->offload_fwd_mark = br_switchdev_mark_get(p->br, p->dev);
  34. return 0;
  35. }
  36. void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
  37. struct sk_buff *skb)
  38. {
  39. if (skb->offload_fwd_mark && !WARN_ON_ONCE(!p->offload_fwd_mark))
  40. BR_INPUT_SKB_CB(skb)->offload_fwd_mark = p->offload_fwd_mark;
  41. }
  42. bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
  43. const struct sk_buff *skb)
  44. {
  45. return !skb->offload_fwd_mark ||
  46. BR_INPUT_SKB_CB(skb)->offload_fwd_mark != p->offload_fwd_mark;
  47. }
  48. /* Flags that can be offloaded to hardware */
  49. #define BR_PORT_FLAGS_HW_OFFLOAD (BR_LEARNING | BR_FLOOD | \
  50. BR_MCAST_FLOOD | BR_BCAST_FLOOD)
  51. int br_switchdev_set_port_flag(struct net_bridge_port *p,
  52. unsigned long flags,
  53. unsigned long mask)
  54. {
  55. struct switchdev_attr attr = {
  56. .orig_dev = p->dev,
  57. .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT,
  58. };
  59. int err;
  60. if (mask & ~BR_PORT_FLAGS_HW_OFFLOAD)
  61. return 0;
  62. err = switchdev_port_attr_get(p->dev, &attr);
  63. if (err == -EOPNOTSUPP)
  64. return 0;
  65. if (err)
  66. return err;
  67. /* Check if specific bridge flag attribute offload is supported */
  68. if (!(attr.u.brport_flags_support & mask)) {
  69. br_warn(p->br, "bridge flag offload is not supported %u(%s)\n",
  70. (unsigned int)p->port_no, p->dev->name);
  71. return -EOPNOTSUPP;
  72. }
  73. attr.id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS;
  74. attr.flags = SWITCHDEV_F_DEFER;
  75. attr.u.brport_flags = flags;
  76. err = switchdev_port_attr_set(p->dev, &attr);
  77. if (err) {
  78. br_warn(p->br, "error setting offload flag on port %u(%s)\n",
  79. (unsigned int)p->port_no, p->dev->name);
  80. return err;
  81. }
  82. return 0;
  83. }
  84. static void
  85. br_switchdev_fdb_call_notifiers(bool adding, const unsigned char *mac,
  86. u16 vid, struct net_device *dev,
  87. bool added_by_user)
  88. {
  89. struct switchdev_notifier_fdb_info info;
  90. unsigned long notifier_type;
  91. info.addr = mac;
  92. info.vid = vid;
  93. info.added_by_user = added_by_user;
  94. notifier_type = adding ? SWITCHDEV_FDB_ADD_TO_DEVICE : SWITCHDEV_FDB_DEL_TO_DEVICE;
  95. call_switchdev_notifiers(notifier_type, dev, &info.info);
  96. }
  97. void
  98. br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, int type)
  99. {
  100. if (!fdb->dst)
  101. return;
  102. switch (type) {
  103. case RTM_DELNEIGH:
  104. br_switchdev_fdb_call_notifiers(false, fdb->key.addr.addr,
  105. fdb->key.vlan_id,
  106. fdb->dst->dev,
  107. fdb->added_by_user);
  108. break;
  109. case RTM_NEWNEIGH:
  110. br_switchdev_fdb_call_notifiers(true, fdb->key.addr.addr,
  111. fdb->key.vlan_id,
  112. fdb->dst->dev,
  113. fdb->added_by_user);
  114. break;
  115. }
  116. }
  117. int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags)
  118. {
  119. struct switchdev_obj_port_vlan v = {
  120. .obj.orig_dev = dev,
  121. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  122. .flags = flags,
  123. .vid_begin = vid,
  124. .vid_end = vid,
  125. };
  126. return switchdev_port_obj_add(dev, &v.obj);
  127. }
  128. int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
  129. {
  130. struct switchdev_obj_port_vlan v = {
  131. .obj.orig_dev = dev,
  132. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  133. .vid_begin = vid,
  134. .vid_end = vid,
  135. };
  136. return switchdev_port_obj_del(dev, &v.obj);
  137. }