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