blob: edfaae7b596726e4c93eb198c7f9edc8a97fc485 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00002/*
3 * net/dsa/dsa_priv.h - Hardware switch handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00004 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 */
6
7#ifndef __DSA_PRIV_H
8#define __DSA_PRIV_H
9
Vladimir Oltean412a1522020-09-23 14:40:37 -070010#include <linux/if_bridge.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000011#include <linux/phy.h>
Alexander Duyck50753142014-09-15 13:00:19 -040012#include <linux/netdevice.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070013#include <linux/netpoll.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040014#include <net/dsa.h>
Alexander Lobakine131a562020-04-21 16:41:08 +030015#include <net/gro_cells.h>
Alexander Duyck50753142014-09-15 13:00:19 -040016
Vladimir Oltean123abc062021-07-22 18:55:40 +030017#define DSA_MAX_NUM_OFFLOADING_BRIDGES BITS_PER_LONG
18
Vivien Didelot52c96f92017-05-19 17:00:51 -040019enum {
Vivien Didelot1faabf72017-05-19 17:00:52 -040020 DSA_NOTIFIER_AGEING_TIME,
Vivien Didelot52c96f92017-05-19 17:00:51 -040021 DSA_NOTIFIER_BRIDGE_JOIN,
22 DSA_NOTIFIER_BRIDGE_LEAVE,
Vivien Didelot685fb6a2017-05-19 17:00:53 -040023 DSA_NOTIFIER_FDB_ADD,
24 DSA_NOTIFIER_FDB_DEL,
Vladimir Oltean3dc80af2021-06-29 17:06:51 +030025 DSA_NOTIFIER_HOST_FDB_ADD,
26 DSA_NOTIFIER_HOST_FDB_DEL,
George McCollister18596f52021-02-09 19:02:12 -060027 DSA_NOTIFIER_HSR_JOIN,
28 DSA_NOTIFIER_HSR_LEAVE,
Tobias Waldekranz058102a2021-01-13 09:42:53 +010029 DSA_NOTIFIER_LAG_CHANGE,
30 DSA_NOTIFIER_LAG_JOIN,
31 DSA_NOTIFIER_LAG_LEAVE,
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -040032 DSA_NOTIFIER_MDB_ADD,
33 DSA_NOTIFIER_MDB_DEL,
Vladimir Olteanb8e997c2021-06-29 17:06:49 +030034 DSA_NOTIFIER_HOST_MDB_ADD,
35 DSA_NOTIFIER_HOST_MDB_DEL,
Vivien Didelotd0c627b2017-05-19 17:00:55 -040036 DSA_NOTIFIER_VLAN_ADD,
37 DSA_NOTIFIER_VLAN_DEL,
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020038 DSA_NOTIFIER_MTU,
Vladimir Oltean53da0eb2021-01-29 03:00:06 +020039 DSA_NOTIFIER_TAG_PROTO,
Vladimir Olteandc452a42021-12-10 01:34:37 +020040 DSA_NOTIFIER_TAG_PROTO_CONNECT,
Vladimir Oltean7f297312021-12-14 03:45:36 +020041 DSA_NOTIFIER_TAG_PROTO_DISCONNECT,
Horatiu Vulturc595c432021-02-16 22:42:04 +010042 DSA_NOTIFIER_MRP_ADD,
43 DSA_NOTIFIER_MRP_DEL,
44 DSA_NOTIFIER_MRP_ADD_RING_ROLE,
45 DSA_NOTIFIER_MRP_DEL_RING_ROLE,
Vladimir Olteanc64b9c02021-07-19 20:14:52 +030046 DSA_NOTIFIER_TAG_8021Q_VLAN_ADD,
47 DSA_NOTIFIER_TAG_8021Q_VLAN_DEL,
Vivien Didelot52c96f92017-05-19 17:00:51 -040048};
49
Vivien Didelot1faabf72017-05-19 17:00:52 -040050/* DSA_NOTIFIER_AGEING_TIME */
51struct dsa_notifier_ageing_time_info {
Vivien Didelot1faabf72017-05-19 17:00:52 -040052 unsigned int ageing_time;
Vivien Didelot1faabf72017-05-19 17:00:52 -040053};
54
Vivien Didelot52c96f92017-05-19 17:00:51 -040055/* DSA_NOTIFIER_BRIDGE_* */
56struct dsa_notifier_bridge_info {
Vladimir Olteand3eed0e2021-12-06 18:57:56 +020057 struct dsa_bridge bridge;
Vladimir Olteanf66a6a62020-05-10 19:37:41 +030058 int tree_index;
Vivien Didelot52c96f92017-05-19 17:00:51 -040059 int sw_index;
60 int port;
Vladimir Olteanb0799222021-12-06 18:57:57 +020061 bool tx_fwd_offload;
Vivien Didelot52c96f92017-05-19 17:00:51 -040062};
63
Vivien Didelot685fb6a2017-05-19 17:00:53 -040064/* DSA_NOTIFIER_FDB_* */
65struct dsa_notifier_fdb_info {
Vivien Didelot685fb6a2017-05-19 17:00:53 -040066 int sw_index;
67 int port;
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +030068 const unsigned char *addr;
69 u16 vid;
Vivien Didelot685fb6a2017-05-19 17:00:53 -040070};
71
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -040072/* DSA_NOTIFIER_MDB_* */
73struct dsa_notifier_mdb_info {
74 const struct switchdev_obj_port_mdb *mdb;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -040075 int sw_index;
76 int port;
77};
78
Tobias Waldekranz058102a2021-01-13 09:42:53 +010079/* DSA_NOTIFIER_LAG_* */
80struct dsa_notifier_lag_info {
81 struct net_device *lag;
82 int sw_index;
83 int port;
84
85 struct netdev_lag_upper_info *info;
86};
87
Vivien Didelotd0c627b2017-05-19 17:00:55 -040088/* DSA_NOTIFIER_VLAN_* */
89struct dsa_notifier_vlan_info {
90 const struct switchdev_obj_port_vlan *vlan;
Vivien Didelotd0c627b2017-05-19 17:00:55 -040091 int sw_index;
92 int port;
Vladimir Oltean31046a52021-02-13 22:43:18 +020093 struct netlink_ext_ack *extack;
Vivien Didelotd0c627b2017-05-19 17:00:55 -040094};
95
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020096/* DSA_NOTIFIER_MTU */
97struct dsa_notifier_mtu_info {
Vladimir Oltean88faba22021-06-21 19:42:18 +030098 bool targeted_match;
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020099 int sw_index;
100 int port;
101 int mtu;
102};
103
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200104/* DSA_NOTIFIER_TAG_PROTO_* */
105struct dsa_notifier_tag_proto_info {
106 const struct dsa_device_ops *tag_ops;
107};
108
Horatiu Vulturc595c432021-02-16 22:42:04 +0100109/* DSA_NOTIFIER_MRP_* */
110struct dsa_notifier_mrp_info {
111 const struct switchdev_obj_mrp *mrp;
112 int sw_index;
113 int port;
114};
115
116/* DSA_NOTIFIER_MRP_* */
117struct dsa_notifier_mrp_ring_role_info {
118 const struct switchdev_obj_ring_role_mrp *mrp;
119 int sw_index;
120 int port;
121};
122
Vladimir Olteanc64b9c02021-07-19 20:14:52 +0300123/* DSA_NOTIFIER_TAG_8021Q_VLAN_* */
124struct dsa_notifier_tag_8021q_vlan_info {
125 int tree_index;
126 int sw_index;
127 int port;
128 u16 vid;
129};
130
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +0200131struct dsa_switchdev_event_work {
132 struct dsa_switch *ds;
133 int port;
Vladimir Oltean4bed3972021-06-29 17:06:57 +0300134 struct net_device *dev;
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +0200135 struct work_struct work;
136 unsigned long event;
137 /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
138 * SWITCHDEV_FDB_DEL_TO_DEVICE
139 */
140 unsigned char addr[ETH_ALEN];
141 u16 vid;
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300142 bool host_addr;
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +0200143};
144
George McCollister18596f52021-02-09 19:02:12 -0600145/* DSA_NOTIFIER_HSR_* */
146struct dsa_notifier_hsr_info {
147 struct net_device *hsr;
148 int sw_index;
149 int port;
150};
151
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152struct dsa_slave_priv {
Vivien Didelot15240242017-09-29 17:19:18 -0400153 /* Copy of CPU port xmit for faster access in slave transmit hot path */
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700154 struct sk_buff * (*xmit)(struct sk_buff *skb,
Alexander Duyck50753142014-09-15 13:00:19 -0400155 struct net_device *dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000156
Alexander Lobakine131a562020-04-21 16:41:08 +0300157 struct gro_cells gcells;
158
Vivien Didelotafdcf152017-01-27 15:29:39 -0500159 /* DSA port data, such as switch, port index, etc. */
160 struct dsa_port *dp;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000161
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700162#ifdef CONFIG_NET_POLL_CONTROLLER
163 struct netpoll *netpoll;
164#endif
Florian Fainellif50f2122017-01-30 12:41:40 -0800165
166 /* TC context */
167 struct list_head mall_tc_list;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000168};
169
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000170/* dsa.c */
Andrew Lunnc39e2a12019-04-28 19:37:18 +0200171const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol);
Andrew Lunn4dad81e2019-04-28 19:37:19 +0200172void dsa_tag_driver_put(const struct dsa_device_ops *ops);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200173const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf);
Andrew Lunnc39e2a12019-04-28 19:37:18 +0200174
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300175bool dsa_schedule_work(struct work_struct *work);
Vladimir Olteana57d8c212021-09-14 16:47:26 +0300176void dsa_flush_workqueue(void);
Florian Fainelli98cdb482018-09-07 11:09:02 -0700177const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000178
Vladimir Oltean4e500252021-06-11 22:01:24 +0300179static inline int dsa_tag_protocol_overhead(const struct dsa_device_ops *ops)
180{
181 return ops->needed_headroom + ops->needed_tailroom;
182}
183
Vivien Didelotf2f23562017-09-19 11:57:00 -0400184/* master.c */
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500185int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
186void dsa_master_teardown(struct net_device *dev);
Vivien Didelotf2f23562017-09-19 11:57:00 -0400187
Vivien Didelot2231c432017-10-16 11:12:17 -0400188static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
189 int device, int port)
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400190{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400191 struct dsa_port *cpu_dp = dev->dsa_ptr;
192 struct dsa_switch_tree *dst = cpu_dp->dst;
Vivien Didelot7b9a2f42019-10-21 16:51:18 -0400193 struct dsa_port *dp;
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400194
Vivien Didelot7b9a2f42019-10-21 16:51:18 -0400195 list_for_each_entry(dp, &dst->ports, list)
196 if (dp->ds->index == device && dp->index == port &&
197 dp->type == DSA_PORT_TYPE_USER)
198 return dp->slave;
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400199
Vivien Didelot7b9a2f42019-10-21 16:51:18 -0400200 return NULL;
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400201}
202
Vivien Didelota40c1752017-05-19 17:00:44 -0400203/* port.c */
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200204void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
205 const struct dsa_device_ops *tag_ops);
Vladimir Oltean39f32102021-08-08 14:16:37 +0300206int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age);
Russell King8640f8d2020-03-03 15:01:46 +0000207int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400208int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy);
Russell King8640f8d2020-03-03 15:01:46 +0000209void dsa_port_disable_rt(struct dsa_port *dp);
Andrew Lunn75104db2019-02-24 20:44:43 +0100210void dsa_port_disable(struct dsa_port *dp);
Vladimir Oltean2afc5262021-03-23 01:51:48 +0200211int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
212 struct netlink_ext_ack *extack);
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300213void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br);
Vivien Didelotcfbed322017-05-19 17:00:45 -0400214void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br);
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100215int dsa_port_lag_change(struct dsa_port *dp,
216 struct netdev_lag_lower_state_info *linfo);
217int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
Vladimir Oltean2afc5262021-03-23 01:51:48 +0200218 struct netdev_lag_upper_info *uinfo,
219 struct netlink_ext_ack *extack);
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300220void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100221void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200222int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
223 struct netlink_ext_ack *extack);
Russell King54a0ed02020-05-12 20:20:25 +0300224bool dsa_port_skip_vlan_configuration(struct dsa_port *dp);
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200225int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock);
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200226int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
Vladimir Oltean88faba22021-06-21 19:42:18 +0300227 bool targeted_match);
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300228int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
229 u16 vid);
230int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
231 u16 vid);
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300232int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
233 u16 vid);
234int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
235 u16 vid);
Vivien Didelotde40fc52017-09-20 19:32:14 -0400236int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data);
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100237int dsa_port_mdb_add(const struct dsa_port *dp,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200238 const struct switchdev_obj_port_mdb *mdb);
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100239int dsa_port_mdb_del(const struct dsa_port *dp,
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400240 const struct switchdev_obj_port_mdb *mdb);
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300241int dsa_port_host_mdb_add(const struct dsa_port *dp,
242 const struct switchdev_obj_port_mdb *mdb);
243int dsa_port_host_mdb_del(const struct dsa_port *dp,
244 const struct switchdev_obj_port_mdb *mdb);
Vladimir Olteane18f4c12021-02-12 17:15:55 +0200245int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200246 struct switchdev_brport_flags flags,
247 struct netlink_ext_ack *extack);
Vladimir Oltean045c45d2021-08-08 17:35:23 +0300248int dsa_port_bridge_flags(struct dsa_port *dp,
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200249 struct switchdev_brport_flags flags,
250 struct netlink_ext_ack *extack);
Vivien Didelot076e7132017-05-19 17:00:50 -0400251int dsa_port_vlan_add(struct dsa_port *dp,
Vladimir Oltean31046a52021-02-13 22:43:18 +0200252 const struct switchdev_obj_port_vlan *vlan,
253 struct netlink_ext_ack *extack);
Vivien Didelot076e7132017-05-19 17:00:50 -0400254int dsa_port_vlan_del(struct dsa_port *dp,
255 const struct switchdev_obj_port_vlan *vlan);
Horatiu Vulturc595c432021-02-16 22:42:04 +0100256int dsa_port_mrp_add(const struct dsa_port *dp,
257 const struct switchdev_obj_mrp *mrp);
258int dsa_port_mrp_del(const struct dsa_port *dp,
259 const struct switchdev_obj_mrp *mrp);
260int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
261 const struct switchdev_obj_ring_role_mrp *mrp);
262int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
263 const struct switchdev_obj_ring_role_mrp *mrp);
Russell King (Oracle)21bd64b2021-11-30 13:09:55 +0000264int dsa_port_phylink_create(struct dsa_port *dp);
Sebastian Reichel33615362018-01-23 16:03:46 +0100265int dsa_port_link_register_of(struct dsa_port *dp);
266void dsa_port_link_unregister_of(struct dsa_port *dp);
George McCollister18596f52021-02-09 19:02:12 -0600267int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr);
268void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr);
Vladimir Oltean724395f2021-08-11 16:46:06 +0300269int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast);
270void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast);
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400271
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000272/* slave.c */
Alexander Duyck50753142014-09-15 13:00:19 -0400273extern const struct dsa_device_ops notag_netdev_ops;
Vladimir Oltean010e2692021-03-23 01:51:50 +0200274extern struct notifier_block dsa_slave_switchdev_notifier;
275extern struct notifier_block dsa_slave_switchdev_blocking_notifier;
276
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000277void dsa_slave_mii_bus_init(struct dsa_switch *ds);
Vivien Didelot951259aa2017-10-27 15:55:19 -0400278int dsa_slave_create(struct dsa_port *dp);
Neil Armstrongcda5c152015-12-07 13:57:35 +0100279void dsa_slave_destroy(struct net_device *slave_dev);
Florian Fainelli24462542014-09-18 17:31:22 -0700280int dsa_slave_suspend(struct net_device *slave_dev);
281int dsa_slave_resume(struct net_device *slave_dev);
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500282int dsa_slave_register_notifier(void);
283void dsa_slave_unregister_notifier(void);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200284void dsa_slave_setup_tagger(struct net_device *slave);
285int dsa_slave_change_mtu(struct net_device *dev, int new_mtu);
Vladimir Oltean06cfb2d2021-08-24 00:22:57 +0300286int dsa_slave_manage_vlan_filtering(struct net_device *dev,
287 bool vlan_filtering);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000288
Vivien Didelotd9450972017-10-16 11:12:15 -0400289static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
290{
291 struct dsa_slave_priv *p = netdev_priv(dev);
292
293 return p->dp;
294}
295
Vivien Didelotd0006b02017-10-16 11:12:16 -0400296static inline struct net_device *
297dsa_slave_to_master(const struct net_device *dev)
298{
299 struct dsa_port *dp = dsa_slave_to_port(dev);
300
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400301 return dp->cpu_dp->master;
Vivien Didelotd0006b02017-10-16 11:12:16 -0400302}
303
Vladimir Oltean412a1522020-09-23 14:40:37 -0700304/* If under a bridge with vlan_filtering=0, make sure to send pvid-tagged
305 * frames as untagged, since the bridge will not untag them.
306 */
307static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
308{
309 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
Vladimir Oltean36cbf392021-12-06 18:57:52 +0200310 struct net_device *br = dsa_port_bridge_dev_get(dp);
Vladimir Oltean412a1522020-09-23 14:40:37 -0700311 struct net_device *dev = skb->dev;
312 struct net_device *upper_dev;
Vladimir Oltean412a1522020-09-23 14:40:37 -0700313 u16 vid, pvid, proto;
314 int err;
315
316 if (!br || br_vlan_enabled(br))
317 return skb;
318
319 err = br_vlan_get_proto(br, &proto);
320 if (err)
321 return skb;
322
323 /* Move VLAN tag from data to hwaccel */
Florian Fainellia3482922020-10-01 19:42:14 -0700324 if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
Vladimir Oltean412a1522020-09-23 14:40:37 -0700325 skb = skb_vlan_untag(skb);
326 if (!skb)
327 return NULL;
328 }
329
330 if (!skb_vlan_tag_present(skb))
331 return skb;
332
333 vid = skb_vlan_tag_get_id(skb);
334
335 /* We already run under an RCU read-side critical section since
336 * we are called from netif_receive_skb_list_internal().
337 */
338 err = br_vlan_get_pvid_rcu(dev, &pvid);
339 if (err)
340 return skb;
341
342 if (vid != pvid)
343 return skb;
344
345 /* The sad part about attempting to untag from DSA is that we
346 * don't know, unless we check, if the skb will end up in
347 * the bridge's data path - br_allowed_ingress() - or not.
348 * For example, there might be an 8021q upper for the
349 * default_pvid of the bridge, which will steal VLAN-tagged traffic
350 * from the bridge's data path. This is a configuration that DSA
351 * supports because vlan_filtering is 0. In that case, we should
352 * definitely keep the tag, to make sure it keeps working.
353 */
Florian Fainelli3a688442020-10-01 19:42:15 -0700354 upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
355 if (upper_dev)
356 return skb;
Vladimir Oltean412a1522020-09-23 14:40:37 -0700357
358 __vlan_hwaccel_clear_tag(skb);
359
360 return skb;
361}
362
Vladimir Oltean884be122021-07-26 19:55:34 +0300363/* For switches without hardware support for DSA tagging to be able
364 * to support termination through the bridge.
365 */
366static inline struct net_device *
367dsa_find_designated_bridge_port_by_vid(struct net_device *master, u16 vid)
368{
369 struct dsa_port *cpu_dp = master->dsa_ptr;
370 struct dsa_switch_tree *dst = cpu_dp->dst;
371 struct bridge_vlan_info vinfo;
372 struct net_device *slave;
373 struct dsa_port *dp;
374 int err;
375
376 list_for_each_entry(dp, &dst->ports, list) {
377 if (dp->type != DSA_PORT_TYPE_USER)
378 continue;
379
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200380 if (!dp->bridge)
Vladimir Oltean884be122021-07-26 19:55:34 +0300381 continue;
382
383 if (dp->stp_state != BR_STATE_LEARNING &&
384 dp->stp_state != BR_STATE_FORWARDING)
385 continue;
386
387 /* Since the bridge might learn this packet, keep the CPU port
388 * affinity with the port that will be used for the reply on
389 * xmit.
390 */
391 if (dp->cpu_dp != cpu_dp)
392 continue;
393
394 slave = dp->slave;
395
396 err = br_vlan_get_info_rcu(slave, vid, &vinfo);
397 if (err)
398 continue;
399
400 return slave;
401 }
402
403 return NULL;
404}
405
Vladimir Olteanbea79072021-07-29 17:56:00 +0300406/* If the ingress port offloads the bridge, we mark the frame as autonomously
407 * forwarded by hardware, so the software bridge doesn't forward in twice, back
408 * to us, because we already did. However, if we're in fallback mode and we do
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200409 * software bridging, we are not offloading it, therefore the dp->bridge
Vladimir Olteanbea79072021-07-29 17:56:00 +0300410 * pointer is not populated, and flooding needs to be done by software (we are
411 * effectively operating in standalone ports mode).
412 */
413static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb)
414{
415 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
416
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200417 skb->offload_fwd_mark = !!(dp->bridge);
Vladimir Olteanbea79072021-07-29 17:56:00 +0300418}
419
Vladimir Olteanf1dacd72021-08-10 16:13:53 +0300420/* Helper for removing DSA header tags from packets in the RX path.
421 * Must not be called before skb_pull(len).
422 * skb->data
423 * |
424 * v
425 * | | | | | | | | | | | | | | | | | | |
426 * +-----------------------+-----------------------+---------------+-------+
427 * | Destination MAC | Source MAC | DSA header | EType |
428 * +-----------------------+-----------------------+---------------+-------+
429 * | |
430 * <----- len -----> <----- len ----->
431 * |
432 * >>>>>>> v
433 * >>>>>>> | | | | | | | | | | | | | | |
434 * >>>>>>> +-----------------------+-----------------------+-------+
435 * >>>>>>> | Destination MAC | Source MAC | EType |
436 * +-----------------------+-----------------------+-------+
437 * ^
438 * |
439 * skb->data
440 */
441static inline void dsa_strip_etype_header(struct sk_buff *skb, int len)
442{
443 memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - len, 2 * ETH_ALEN);
444}
445
Vladimir Oltean6bef7942021-08-10 16:13:54 +0300446/* Helper for creating space for DSA header tags in TX path packets.
447 * Must not be called before skb_push(len).
448 *
449 * Before:
450 *
451 * <<<<<<< | | | | | | | | | | | | | | |
452 * ^ <<<<<<< +-----------------------+-----------------------+-------+
453 * | <<<<<<< | Destination MAC | Source MAC | EType |
454 * | +-----------------------+-----------------------+-------+
455 * <----- len ----->
456 * |
457 * |
458 * skb->data
459 *
460 * After:
461 *
462 * | | | | | | | | | | | | | | | | | | |
463 * +-----------------------+-----------------------+---------------+-------+
464 * | Destination MAC | Source MAC | DSA header | EType |
465 * +-----------------------+-----------------------+---------------+-------+
466 * ^ | |
467 * | <----- len ----->
468 * skb->data
469 */
470static inline void dsa_alloc_etype_header(struct sk_buff *skb, int len)
471{
472 memmove(skb->data, skb->data + len, 2 * ETH_ALEN);
473}
474
Vladimir Oltean5d928ff2021-08-10 16:13:55 +0300475/* On RX, eth_type_trans() on the DSA master pulls ETH_HLEN bytes starting from
476 * skb_mac_header(skb), which leaves skb->data pointing at the first byte after
477 * what the DSA master perceives as the EtherType (the beginning of the L3
478 * protocol). Since DSA EtherType header taggers treat the EtherType as part of
479 * the DSA tag itself, and the EtherType is 2 bytes in length, the DSA header
480 * is located 2 bytes behind skb->data. Note that EtherType in this context
481 * means the first 2 bytes of the DSA header, not the encapsulated EtherType
482 * that will become visible after the DSA header is stripped.
483 */
484static inline void *dsa_etype_header_pos_rx(struct sk_buff *skb)
485{
486 return skb->data - 2;
487}
488
Vladimir Olteana72808b2021-08-10 16:13:56 +0300489/* On TX, skb->data points to skb_mac_header(skb), which means that EtherType
490 * header taggers start exactly where the EtherType is (the EtherType is
491 * treated as part of the DSA header).
492 */
493static inline void *dsa_etype_header_pos_tx(struct sk_buff *skb)
494{
495 return skb->data + 2 * ETH_ALEN;
496}
497
Vivien Didelotf515f192017-02-03 13:20:20 -0500498/* switch.c */
499int dsa_switch_register_notifier(struct dsa_switch *ds);
500void dsa_switch_unregister_notifier(struct dsa_switch *ds);
Vladimir Olteanbff33f72020-03-27 21:55:43 +0200501
502/* dsa2.c */
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100503void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag);
504void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag);
Vladimir Oltean886f8e22021-01-29 03:00:04 +0200505int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v);
506int dsa_broadcast(unsigned long e, void *v);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200507int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
508 struct net_device *master,
509 const struct dsa_device_ops *tag_ops,
510 const struct dsa_device_ops *old_tag_ops);
Vladimir Oltean3f9bb032021-12-06 18:57:47 +0200511unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max);
512void dsa_bridge_num_put(const struct net_device *bridge_dev,
513 unsigned int bridge_num);
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200514struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
515 const struct net_device *br);
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100516
Vladimir Olteane19cc132021-07-19 20:14:51 +0300517/* tag_8021q.c */
518int dsa_tag_8021q_bridge_join(struct dsa_switch *ds,
519 struct dsa_notifier_bridge_info *info);
520int dsa_tag_8021q_bridge_leave(struct dsa_switch *ds,
521 struct dsa_notifier_bridge_info *info);
Vladimir Olteanc64b9c02021-07-19 20:14:52 +0300522int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
523 struct dsa_notifier_tag_8021q_vlan_info *info);
524int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
525 struct dsa_notifier_tag_8021q_vlan_info *info);
Vladimir Olteane19cc132021-07-19 20:14:51 +0300526
Vladimir Olteanbff33f72020-03-27 21:55:43 +0200527extern struct list_head dsa_tree_list;
528
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000529#endif