// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2019, Vladimir Oltean <[email protected]> * * This module is not a complete tagger implementation. It only provides * primitives for taggers that rely on 802.1Q VLAN tags to use. */ #include <linux/if_vlan.h> #include <linux/dsa/8021q.h> #include "port.h" #include "switch.h" #include "tag.h" #include "tag_8021q.h" /* Binary structure of the fake 12-bit VID field (when the TPID is * ETH_P_DSA_8021Q): * * | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | * +-----------+-----+-----------------+-----------+-----------------------+ * | RSV | VBID| SWITCH_ID | VBID | PORT | * +-----------+-----+-----------------+-----------+-----------------------+ * * RSV - VID[11:10]: * Reserved. Must be set to 3 (0b11). * * SWITCH_ID - VID[8:6]: * Index of switch within DSA tree. Must be between 0 and 7. * * VBID - { VID[9], VID[5:4] }: * Virtual bridge ID. If between 1 and 7, packet targets the broadcast * domain of a bridge. If transmitted as zero, packet targets a single * port. * * PORT - VID[3:0]: * Index of switch port. Must be between 0 and 15. */ #define DSA_8021Q_RSV_VAL … #define DSA_8021Q_RSV_SHIFT … #define DSA_8021Q_RSV_MASK … #define DSA_8021Q_RSV … #define DSA_8021Q_SWITCH_ID_SHIFT … #define DSA_8021Q_SWITCH_ID_MASK … #define DSA_8021Q_SWITCH_ID(x) … #define DSA_8021Q_VBID_HI_SHIFT … #define DSA_8021Q_VBID_HI_MASK … #define DSA_8021Q_VBID_LO_SHIFT … #define DSA_8021Q_VBID_LO_MASK … #define DSA_8021Q_VBID_HI(x) … #define DSA_8021Q_VBID_LO(x) … #define DSA_8021Q_VBID(x) … #define DSA_8021Q_PORT_SHIFT … #define DSA_8021Q_PORT_MASK … #define DSA_8021Q_PORT(x) … struct dsa_tag_8021q_vlan { … }; struct dsa_8021q_context { … }; u16 dsa_tag_8021q_bridge_vid(unsigned int bridge_num) { … } EXPORT_SYMBOL_GPL(…); /* Returns the VID that will be installed as pvid for this switch port, sent as * tagged egress towards the CPU port and decoded by the rcv function. */ u16 dsa_tag_8021q_standalone_vid(const struct dsa_port *dp) { … } EXPORT_SYMBOL_GPL(…); /* Returns the decoded switch ID from the RX VID. */ int dsa_8021q_rx_switch_id(u16 vid) { … } EXPORT_SYMBOL_GPL(…); /* Returns the decoded port ID from the RX VID. */ int dsa_8021q_rx_source_port(u16 vid) { … } EXPORT_SYMBOL_GPL(…); /* Returns the decoded VBID from the RX VID. */ static int dsa_tag_8021q_rx_vbid(u16 vid) { … } bool vid_is_dsa_8021q(u16 vid) { … } EXPORT_SYMBOL_GPL(…); static struct dsa_tag_8021q_vlan * dsa_tag_8021q_vlan_find(struct dsa_8021q_context *ctx, int port, u16 vid) { … } static int dsa_port_do_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, u16 flags) { … } static int dsa_port_do_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid) { … } static bool dsa_port_tag_8021q_vlan_match(struct dsa_port *dp, struct dsa_notifier_tag_8021q_vlan_info *info) { … } int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds, struct dsa_notifier_tag_8021q_vlan_info *info) { … } int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds, struct dsa_notifier_tag_8021q_vlan_info *info) { … } /* There are 2 ways of offloading tag_8021q VLANs. * * One is to use a hardware TCAM to push the port's standalone VLAN into the * frame when forwarding it to the CPU, as an egress modification rule on the * CPU port. This is preferable because it has no side effects for the * autonomous forwarding path, and accomplishes tag_8021q's primary goal of * identifying the source port of each packet based on VLAN ID. * * The other is to commit the tag_8021q VLAN as a PVID to the VLAN table, and * to configure the port as VLAN-unaware. This is less preferable because * unique source port identification can only be done for standalone ports; * under a VLAN-unaware bridge, all ports share the same tag_8021q VLAN as * PVID, and under a VLAN-aware bridge, packets received by software will not * have tag_8021q VLANs appended, just bridge VLANs. * * For tag_8021q implementations of the second type, this method is used to * replace the standalone tag_8021q VLAN of a port with the tag_8021q VLAN to * be used for VLAN-unaware bridging. */ int dsa_tag_8021q_bridge_join(struct dsa_switch *ds, int port, struct dsa_bridge bridge, bool *tx_fwd_offload, struct netlink_ext_ack *extack) { … } EXPORT_SYMBOL_GPL(…); void dsa_tag_8021q_bridge_leave(struct dsa_switch *ds, int port, struct dsa_bridge bridge) { … } EXPORT_SYMBOL_GPL(…); /* Set up a port's standalone tag_8021q VLAN */ static int dsa_tag_8021q_port_setup(struct dsa_switch *ds, int port) { … } static void dsa_tag_8021q_port_teardown(struct dsa_switch *ds, int port) { … } static int dsa_tag_8021q_setup(struct dsa_switch *ds) { … } static void dsa_tag_8021q_teardown(struct dsa_switch *ds) { … } int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto) { … } EXPORT_SYMBOL_GPL(…); void dsa_tag_8021q_unregister(struct dsa_switch *ds) { … } EXPORT_SYMBOL_GPL(…); struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev, u16 tpid, u16 tci) { … } EXPORT_SYMBOL_GPL(…); static struct net_device * dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid) { … } struct net_device *dsa_tag_8021q_find_user(struct net_device *conduit, int source_port, int switch_id, int vid, int vbid) { … } EXPORT_SYMBOL_GPL(…); /** * dsa_8021q_rcv - Decode source information from tag_8021q header * @skb: RX socket buffer * @source_port: pointer to storage for precise source port information. * If this is known already from outside tag_8021q, the pre-initialized * value is preserved. If not known, pass -1. * @switch_id: similar to source_port. * @vbid: pointer to storage for imprecise bridge ID. Must be pre-initialized * with -1. If a positive value is returned, the source_port and switch_id * are invalid. * @vid: pointer to storage for original VID, in case tag_8021q decoding failed. * * If the packet has a tag_8021q header, decode it and set @source_port, * @switch_id and @vbid, and strip the header. Otherwise set @vid and keep the * header in the hwaccel area of the packet. */ void dsa_8021q_rcv(struct sk_buff *skb, int *source_port, int *switch_id, int *vbid, int *vid) { … } EXPORT_SYMBOL_GPL(…);