OSDN Git Service

net: dsa: request drivers to perform FDB isolation
[uclinux-h8/linux.git] / net / dsa / dsa_priv.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * net/dsa/dsa_priv.h - Hardware switch handling
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  */
6
7 #ifndef __DSA_PRIV_H
8 #define __DSA_PRIV_H
9
10 #include <linux/if_bridge.h>
11 #include <linux/if_vlan.h>
12 #include <linux/phy.h>
13 #include <linux/netdevice.h>
14 #include <linux/netpoll.h>
15 #include <net/dsa.h>
16 #include <net/gro_cells.h>
17
18 #define DSA_MAX_NUM_OFFLOADING_BRIDGES          BITS_PER_LONG
19
20 enum {
21         DSA_NOTIFIER_AGEING_TIME,
22         DSA_NOTIFIER_BRIDGE_JOIN,
23         DSA_NOTIFIER_BRIDGE_LEAVE,
24         DSA_NOTIFIER_FDB_ADD,
25         DSA_NOTIFIER_FDB_DEL,
26         DSA_NOTIFIER_HOST_FDB_ADD,
27         DSA_NOTIFIER_HOST_FDB_DEL,
28         DSA_NOTIFIER_LAG_FDB_ADD,
29         DSA_NOTIFIER_LAG_FDB_DEL,
30         DSA_NOTIFIER_LAG_CHANGE,
31         DSA_NOTIFIER_LAG_JOIN,
32         DSA_NOTIFIER_LAG_LEAVE,
33         DSA_NOTIFIER_MDB_ADD,
34         DSA_NOTIFIER_MDB_DEL,
35         DSA_NOTIFIER_HOST_MDB_ADD,
36         DSA_NOTIFIER_HOST_MDB_DEL,
37         DSA_NOTIFIER_VLAN_ADD,
38         DSA_NOTIFIER_VLAN_DEL,
39         DSA_NOTIFIER_HOST_VLAN_ADD,
40         DSA_NOTIFIER_HOST_VLAN_DEL,
41         DSA_NOTIFIER_MTU,
42         DSA_NOTIFIER_TAG_PROTO,
43         DSA_NOTIFIER_TAG_PROTO_CONNECT,
44         DSA_NOTIFIER_TAG_PROTO_DISCONNECT,
45         DSA_NOTIFIER_TAG_8021Q_VLAN_ADD,
46         DSA_NOTIFIER_TAG_8021Q_VLAN_DEL,
47         DSA_NOTIFIER_MASTER_STATE_CHANGE,
48 };
49
50 /* DSA_NOTIFIER_AGEING_TIME */
51 struct dsa_notifier_ageing_time_info {
52         unsigned int ageing_time;
53 };
54
55 /* DSA_NOTIFIER_BRIDGE_* */
56 struct dsa_notifier_bridge_info {
57         struct dsa_bridge bridge;
58         int tree_index;
59         int sw_index;
60         int port;
61         bool tx_fwd_offload;
62 };
63
64 /* DSA_NOTIFIER_FDB_* */
65 struct dsa_notifier_fdb_info {
66         int sw_index;
67         int port;
68         const unsigned char *addr;
69         u16 vid;
70         struct dsa_db db;
71 };
72
73 /* DSA_NOTIFIER_LAG_FDB_* */
74 struct dsa_notifier_lag_fdb_info {
75         struct dsa_lag *lag;
76         const unsigned char *addr;
77         u16 vid;
78         struct dsa_db db;
79 };
80
81 /* DSA_NOTIFIER_MDB_* */
82 struct dsa_notifier_mdb_info {
83         const struct switchdev_obj_port_mdb *mdb;
84         int sw_index;
85         int port;
86         struct dsa_db db;
87 };
88
89 /* DSA_NOTIFIER_LAG_* */
90 struct dsa_notifier_lag_info {
91         struct dsa_lag lag;
92         int sw_index;
93         int port;
94
95         struct netdev_lag_upper_info *info;
96 };
97
98 /* DSA_NOTIFIER_VLAN_* */
99 struct dsa_notifier_vlan_info {
100         const struct switchdev_obj_port_vlan *vlan;
101         int sw_index;
102         int port;
103         struct netlink_ext_ack *extack;
104 };
105
106 /* DSA_NOTIFIER_MTU */
107 struct dsa_notifier_mtu_info {
108         bool targeted_match;
109         int sw_index;
110         int port;
111         int mtu;
112 };
113
114 /* DSA_NOTIFIER_TAG_PROTO_* */
115 struct dsa_notifier_tag_proto_info {
116         const struct dsa_device_ops *tag_ops;
117 };
118
119 /* DSA_NOTIFIER_TAG_8021Q_VLAN_* */
120 struct dsa_notifier_tag_8021q_vlan_info {
121         int tree_index;
122         int sw_index;
123         int port;
124         u16 vid;
125 };
126
127 /* DSA_NOTIFIER_MASTER_STATE_CHANGE */
128 struct dsa_notifier_master_state_info {
129         const struct net_device *master;
130         bool operational;
131 };
132
133 struct dsa_switchdev_event_work {
134         struct net_device *dev;
135         struct net_device *orig_dev;
136         struct work_struct work;
137         unsigned long event;
138         /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
139          * SWITCHDEV_FDB_DEL_TO_DEVICE
140          */
141         unsigned char addr[ETH_ALEN];
142         u16 vid;
143         bool host_addr;
144 };
145
146 struct dsa_slave_priv {
147         /* Copy of CPU port xmit for faster access in slave transmit hot path */
148         struct sk_buff *        (*xmit)(struct sk_buff *skb,
149                                         struct net_device *dev);
150
151         struct gro_cells        gcells;
152
153         /* DSA port data, such as switch, port index, etc. */
154         struct dsa_port         *dp;
155
156 #ifdef CONFIG_NET_POLL_CONTROLLER
157         struct netpoll          *netpoll;
158 #endif
159
160         /* TC context */
161         struct list_head        mall_tc_list;
162 };
163
164 /* dsa.c */
165 const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol);
166 void dsa_tag_driver_put(const struct dsa_device_ops *ops);
167 const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf);
168
169 bool dsa_schedule_work(struct work_struct *work);
170 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
171
172 static inline int dsa_tag_protocol_overhead(const struct dsa_device_ops *ops)
173 {
174         return ops->needed_headroom + ops->needed_tailroom;
175 }
176
177 /* master.c */
178 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
179 void dsa_master_teardown(struct net_device *dev);
180
181 static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
182                                                        int device, int port)
183 {
184         struct dsa_port *cpu_dp = dev->dsa_ptr;
185         struct dsa_switch_tree *dst = cpu_dp->dst;
186         struct dsa_port *dp;
187
188         list_for_each_entry(dp, &dst->ports, list)
189                 if (dp->ds->index == device && dp->index == port &&
190                     dp->type == DSA_PORT_TYPE_USER)
191                         return dp->slave;
192
193         return NULL;
194 }
195
196 /* port.c */
197 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
198                                const struct dsa_device_ops *tag_ops);
199 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age);
200 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy);
201 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy);
202 void dsa_port_disable_rt(struct dsa_port *dp);
203 void dsa_port_disable(struct dsa_port *dp);
204 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
205                          struct netlink_ext_ack *extack);
206 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br);
207 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br);
208 int dsa_port_lag_change(struct dsa_port *dp,
209                         struct netdev_lag_lower_state_info *linfo);
210 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
211                       struct netdev_lag_upper_info *uinfo,
212                       struct netlink_ext_ack *extack);
213 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
214 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
215 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
216                             struct netlink_ext_ack *extack);
217 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp);
218 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock);
219 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
220                         bool targeted_match);
221 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
222                      u16 vid);
223 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
224                      u16 vid);
225 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
226                           u16 vid);
227 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
228                           u16 vid);
229 int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
230                          u16 vid);
231 int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
232                          u16 vid);
233 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data);
234 int dsa_port_mdb_add(const struct dsa_port *dp,
235                      const struct switchdev_obj_port_mdb *mdb);
236 int dsa_port_mdb_del(const struct dsa_port *dp,
237                      const struct switchdev_obj_port_mdb *mdb);
238 int dsa_port_host_mdb_add(const struct dsa_port *dp,
239                           const struct switchdev_obj_port_mdb *mdb);
240 int dsa_port_host_mdb_del(const struct dsa_port *dp,
241                           const struct switchdev_obj_port_mdb *mdb);
242 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
243                               struct switchdev_brport_flags flags,
244                               struct netlink_ext_ack *extack);
245 int dsa_port_bridge_flags(struct dsa_port *dp,
246                           struct switchdev_brport_flags flags,
247                           struct netlink_ext_ack *extack);
248 int dsa_port_vlan_add(struct dsa_port *dp,
249                       const struct switchdev_obj_port_vlan *vlan,
250                       struct netlink_ext_ack *extack);
251 int dsa_port_vlan_del(struct dsa_port *dp,
252                       const struct switchdev_obj_port_vlan *vlan);
253 int dsa_port_host_vlan_add(struct dsa_port *dp,
254                            const struct switchdev_obj_port_vlan *vlan,
255                            struct netlink_ext_ack *extack);
256 int dsa_port_host_vlan_del(struct dsa_port *dp,
257                            const struct switchdev_obj_port_vlan *vlan);
258 int dsa_port_mrp_add(const struct dsa_port *dp,
259                      const struct switchdev_obj_mrp *mrp);
260 int dsa_port_mrp_del(const struct dsa_port *dp,
261                      const struct switchdev_obj_mrp *mrp);
262 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
263                                const struct switchdev_obj_ring_role_mrp *mrp);
264 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
265                                const struct switchdev_obj_ring_role_mrp *mrp);
266 int dsa_port_phylink_create(struct dsa_port *dp);
267 int dsa_port_link_register_of(struct dsa_port *dp);
268 void dsa_port_link_unregister_of(struct dsa_port *dp);
269 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr);
270 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr);
271 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast);
272 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast);
273
274 /* slave.c */
275 extern const struct dsa_device_ops notag_netdev_ops;
276 extern struct notifier_block dsa_slave_switchdev_notifier;
277 extern struct notifier_block dsa_slave_switchdev_blocking_notifier;
278
279 void dsa_slave_mii_bus_init(struct dsa_switch *ds);
280 int dsa_slave_create(struct dsa_port *dp);
281 void dsa_slave_destroy(struct net_device *slave_dev);
282 int dsa_slave_suspend(struct net_device *slave_dev);
283 int dsa_slave_resume(struct net_device *slave_dev);
284 int dsa_slave_register_notifier(void);
285 void dsa_slave_unregister_notifier(void);
286 void dsa_slave_setup_tagger(struct net_device *slave);
287 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu);
288 int dsa_slave_manage_vlan_filtering(struct net_device *dev,
289                                     bool vlan_filtering);
290
291 static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
292 {
293         struct dsa_slave_priv *p = netdev_priv(dev);
294
295         return p->dp;
296 }
297
298 static inline struct net_device *
299 dsa_slave_to_master(const struct net_device *dev)
300 {
301         struct dsa_port *dp = dsa_slave_to_port(dev);
302
303         return dp->cpu_dp->master;
304 }
305
306 /* If under a bridge with vlan_filtering=0, make sure to send pvid-tagged
307  * frames as untagged, since the bridge will not untag them.
308  */
309 static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
310 {
311         struct dsa_port *dp = dsa_slave_to_port(skb->dev);
312         struct net_device *br = dsa_port_bridge_dev_get(dp);
313         struct net_device *dev = skb->dev;
314         struct net_device *upper_dev;
315         u16 vid, pvid, proto;
316         int err;
317
318         if (!br || br_vlan_enabled(br))
319                 return skb;
320
321         err = br_vlan_get_proto(br, &proto);
322         if (err)
323                 return skb;
324
325         /* Move VLAN tag from data to hwaccel */
326         if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
327                 skb = skb_vlan_untag(skb);
328                 if (!skb)
329                         return NULL;
330         }
331
332         if (!skb_vlan_tag_present(skb))
333                 return skb;
334
335         vid = skb_vlan_tag_get_id(skb);
336
337         /* We already run under an RCU read-side critical section since
338          * we are called from netif_receive_skb_list_internal().
339          */
340         err = br_vlan_get_pvid_rcu(dev, &pvid);
341         if (err)
342                 return skb;
343
344         if (vid != pvid)
345                 return skb;
346
347         /* The sad part about attempting to untag from DSA is that we
348          * don't know, unless we check, if the skb will end up in
349          * the bridge's data path - br_allowed_ingress() - or not.
350          * For example, there might be an 8021q upper for the
351          * default_pvid of the bridge, which will steal VLAN-tagged traffic
352          * from the bridge's data path. This is a configuration that DSA
353          * supports because vlan_filtering is 0. In that case, we should
354          * definitely keep the tag, to make sure it keeps working.
355          */
356         upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
357         if (upper_dev)
358                 return skb;
359
360         __vlan_hwaccel_clear_tag(skb);
361
362         return skb;
363 }
364
365 /* For switches without hardware support for DSA tagging to be able
366  * to support termination through the bridge.
367  */
368 static inline struct net_device *
369 dsa_find_designated_bridge_port_by_vid(struct net_device *master, u16 vid)
370 {
371         struct dsa_port *cpu_dp = master->dsa_ptr;
372         struct dsa_switch_tree *dst = cpu_dp->dst;
373         struct bridge_vlan_info vinfo;
374         struct net_device *slave;
375         struct dsa_port *dp;
376         int err;
377
378         list_for_each_entry(dp, &dst->ports, list) {
379                 if (dp->type != DSA_PORT_TYPE_USER)
380                         continue;
381
382                 if (!dp->bridge)
383                         continue;
384
385                 if (dp->stp_state != BR_STATE_LEARNING &&
386                     dp->stp_state != BR_STATE_FORWARDING)
387                         continue;
388
389                 /* Since the bridge might learn this packet, keep the CPU port
390                  * affinity with the port that will be used for the reply on
391                  * xmit.
392                  */
393                 if (dp->cpu_dp != cpu_dp)
394                         continue;
395
396                 slave = dp->slave;
397
398                 err = br_vlan_get_info_rcu(slave, vid, &vinfo);
399                 if (err)
400                         continue;
401
402                 return slave;
403         }
404
405         return NULL;
406 }
407
408 /* If the ingress port offloads the bridge, we mark the frame as autonomously
409  * forwarded by hardware, so the software bridge doesn't forward in twice, back
410  * to us, because we already did. However, if we're in fallback mode and we do
411  * software bridging, we are not offloading it, therefore the dp->bridge
412  * pointer is not populated, and flooding needs to be done by software (we are
413  * effectively operating in standalone ports mode).
414  */
415 static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb)
416 {
417         struct dsa_port *dp = dsa_slave_to_port(skb->dev);
418
419         skb->offload_fwd_mark = !!(dp->bridge);
420 }
421
422 /* Helper for removing DSA header tags from packets in the RX path.
423  * Must not be called before skb_pull(len).
424  *                                                                 skb->data
425  *                                                                         |
426  *                                                                         v
427  * |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
428  * +-----------------------+-----------------------+---------------+-------+
429  * |    Destination MAC    |      Source MAC       |  DSA header   | EType |
430  * +-----------------------+-----------------------+---------------+-------+
431  *                                                 |               |
432  * <----- len ----->                               <----- len ----->
433  *                 |
434  *       >>>>>>>   v
435  *       >>>>>>>   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
436  *       >>>>>>>   +-----------------------+-----------------------+-------+
437  *       >>>>>>>   |    Destination MAC    |      Source MAC       | EType |
438  *                 +-----------------------+-----------------------+-------+
439  *                                                                         ^
440  *                                                                         |
441  *                                                                 skb->data
442  */
443 static inline void dsa_strip_etype_header(struct sk_buff *skb, int len)
444 {
445         memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - len, 2 * ETH_ALEN);
446 }
447
448 /* Helper for creating space for DSA header tags in TX path packets.
449  * Must not be called before skb_push(len).
450  *
451  * Before:
452  *
453  *       <<<<<<<   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
454  * ^     <<<<<<<   +-----------------------+-----------------------+-------+
455  * |     <<<<<<<   |    Destination MAC    |      Source MAC       | EType |
456  * |               +-----------------------+-----------------------+-------+
457  * <----- len ----->
458  * |
459  * |
460  * skb->data
461  *
462  * After:
463  *
464  * |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
465  * +-----------------------+-----------------------+---------------+-------+
466  * |    Destination MAC    |      Source MAC       |  DSA header   | EType |
467  * +-----------------------+-----------------------+---------------+-------+
468  * ^                                               |               |
469  * |                                               <----- len ----->
470  * skb->data
471  */
472 static inline void dsa_alloc_etype_header(struct sk_buff *skb, int len)
473 {
474         memmove(skb->data, skb->data + len, 2 * ETH_ALEN);
475 }
476
477 /* On RX, eth_type_trans() on the DSA master pulls ETH_HLEN bytes starting from
478  * skb_mac_header(skb), which leaves skb->data pointing at the first byte after
479  * what the DSA master perceives as the EtherType (the beginning of the L3
480  * protocol). Since DSA EtherType header taggers treat the EtherType as part of
481  * the DSA tag itself, and the EtherType is 2 bytes in length, the DSA header
482  * is located 2 bytes behind skb->data. Note that EtherType in this context
483  * means the first 2 bytes of the DSA header, not the encapsulated EtherType
484  * that will become visible after the DSA header is stripped.
485  */
486 static inline void *dsa_etype_header_pos_rx(struct sk_buff *skb)
487 {
488         return skb->data - 2;
489 }
490
491 /* On TX, skb->data points to skb_mac_header(skb), which means that EtherType
492  * header taggers start exactly where the EtherType is (the EtherType is
493  * treated as part of the DSA header).
494  */
495 static inline void *dsa_etype_header_pos_tx(struct sk_buff *skb)
496 {
497         return skb->data + 2 * ETH_ALEN;
498 }
499
500 /* switch.c */
501 int dsa_switch_register_notifier(struct dsa_switch *ds);
502 void dsa_switch_unregister_notifier(struct dsa_switch *ds);
503
504 /* dsa2.c */
505 void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag);
506 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag);
507 struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
508                                   const struct net_device *lag_dev);
509 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v);
510 int dsa_broadcast(unsigned long e, void *v);
511 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
512                               struct net_device *master,
513                               const struct dsa_device_ops *tag_ops,
514                               const struct dsa_device_ops *old_tag_ops);
515 void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
516                                         struct net_device *master,
517                                         bool up);
518 void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
519                                        struct net_device *master,
520                                        bool up);
521 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max);
522 void dsa_bridge_num_put(const struct net_device *bridge_dev,
523                         unsigned int bridge_num);
524 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
525                                         const struct net_device *br);
526
527 /* tag_8021q.c */
528 int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
529                                   struct dsa_notifier_tag_8021q_vlan_info *info);
530 int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
531                                   struct dsa_notifier_tag_8021q_vlan_info *info);
532
533 extern struct list_head dsa_tree_list;
534
535 #endif