br_switchdev.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 (netdev_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 netdev_phys_item_id ppid = { };
  22. int err;
  23. ASSERT_RTNL();
  24. err = dev_get_port_parent_id(p->dev, &ppid, true);
  25. if (err) {
  26. if (err == -EOPNOTSUPP)
  27. return 0;
  28. return err;
  29. }
  30. p->offload_fwd_mark = br_switchdev_mark_get(p->br, p->dev);
  31. return 0;
  32. }
  33. void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
  34. struct sk_buff *skb)
  35. {
  36. if (skb->offload_fwd_mark && !WARN_ON_ONCE(!p->offload_fwd_mark))
  37. BR_INPUT_SKB_CB(skb)->offload_fwd_mark = p->offload_fwd_mark;
  38. }
  39. bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
  40. const struct sk_buff *skb)
  41. {
  42. return !skb->offload_fwd_mark ||
  43. BR_INPUT_SKB_CB(skb)->offload_fwd_mark != p->offload_fwd_mark;
  44. }
  45. /* Flags that can be offloaded to hardware */
  46. #define BR_PORT_FLAGS_HW_OFFLOAD (BR_LEARNING | BR_FLOOD | \
  47. BR_MCAST_FLOOD | BR_BCAST_FLOOD)
  48. int br_switchdev_set_port_flag(struct net_bridge_port *p,
  49. unsigned long flags,
  50. unsigned long mask)
  51. {
  52. struct switchdev_attr attr = {
  53. .orig_dev = p->dev,
  54. .id = SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS,
  55. .u.brport_flags = mask,
  56. };
  57. struct switchdev_notifier_port_attr_info info = {
  58. .attr = &attr,
  59. };
  60. int err;
  61. if (mask & ~BR_PORT_FLAGS_HW_OFFLOAD)
  62. return 0;
  63. /* We run from atomic context here */
  64. err = call_switchdev_notifiers(SWITCHDEV_PORT_ATTR_SET, p->dev,
  65. &info.info, NULL);
  66. err = notifier_to_errno(err);
  67. if (err == -EOPNOTSUPP)
  68. return 0;
  69. if (err) {
  70. br_warn(p->br, "bridge flag offload is not supported %u(%s)\n",
  71. (unsigned int)p->port_no, p->dev->name);
  72. return -EOPNOTSUPP;
  73. }
  74. attr.id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS;
  75. attr.flags = SWITCHDEV_F_DEFER;
  76. attr.u.brport_flags = flags;
  77. err = switchdev_port_attr_set(p->dev, &attr);
  78. if (err) {
  79. br_warn(p->br, "error setting offload flag on port %u(%s)\n",
  80. (unsigned int)p->port_no, p->dev->name);
  81. return err;
  82. }
  83. return 0;
  84. }
  85. static void
  86. br_switchdev_fdb_call_notifiers(bool adding, const unsigned char *mac,
  87. u16 vid, struct net_device *dev,
  88. bool added_by_user, bool offloaded)
  89. {
  90. struct switchdev_notifier_fdb_info info;
  91. unsigned long notifier_type;
  92. info.addr = mac;
  93. info.vid = vid;
  94. info.added_by_user = added_by_user;
  95. info.offloaded = offloaded;
  96. notifier_type = adding ? SWITCHDEV_FDB_ADD_TO_DEVICE : SWITCHDEV_FDB_DEL_TO_DEVICE;
  97. call_switchdev_notifiers(notifier_type, dev, &info.info, NULL);
  98. }
  99. void
  100. br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, int type)
  101. {
  102. if (!fdb->dst)
  103. return;
  104. if (test_bit(BR_FDB_LOCAL, &fdb->flags))
  105. return;
  106. switch (type) {
  107. case RTM_DELNEIGH:
  108. br_switchdev_fdb_call_notifiers(false, fdb->key.addr.addr,
  109. fdb->key.vlan_id,
  110. fdb->dst->dev,
  111. test_bit(BR_FDB_ADDED_BY_USER,
  112. &fdb->flags),
  113. test_bit(BR_FDB_OFFLOADED,
  114. &fdb->flags));
  115. break;
  116. case RTM_NEWNEIGH:
  117. br_switchdev_fdb_call_notifiers(true, fdb->key.addr.addr,
  118. fdb->key.vlan_id,
  119. fdb->dst->dev,
  120. test_bit(BR_FDB_ADDED_BY_USER,
  121. &fdb->flags),
  122. test_bit(BR_FDB_OFFLOADED,
  123. &fdb->flags));
  124. break;
  125. }
  126. }
  127. int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
  128. struct netlink_ext_ack *extack)
  129. {
  130. struct switchdev_obj_port_vlan v = {
  131. .obj.orig_dev = dev,
  132. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  133. .flags = flags,
  134. .vid_begin = vid,
  135. .vid_end = vid,
  136. };
  137. return switchdev_port_obj_add(dev, &v.obj, extack);
  138. }
  139. int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
  140. {
  141. struct switchdev_obj_port_vlan v = {
  142. .obj.orig_dev = dev,
  143. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  144. .vid_begin = vid,
  145. .vid_end = vid,
  146. };
  147. return switchdev_port_obj_del(dev, &v.obj);
  148. }