Skip to content

Commit 3eaae1d

Browse files
vladimirolteandavem330
authored andcommitted
net: dsa: tag_8021q: support up to 8 VLANs per port using sub-VLANs
For switches that support VLAN retagging, such as sja1105, we extend dsa_8021q by encoding a "sub-VLAN" into the remaining 3 free bits in the dsa_8021q tag. A sub-VLAN is nothing more than a number in the range 0-7, which serves as an index into a per-port driver lookup table. The sub-VLAN value of zero means that traffic is untagged (this is also backwards-compatible with dsa_8021q without retagging). The switch should be configured to retag VLAN-tagged traffic that gets transmitted towards the CPU port (and towards the CPU only). Example: bridge vlan add dev sw1p0 vid 100 The switch retags frames received on port 0, going to the CPU, and having VID 100, to the VID of 1104 (0x0450). In dsa_8021q language: | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +-----------+-----+-----------------+-----------+-----------------------+ | DIR | SVL | SWITCH_ID | SUBVLAN | PORT | +-----------+-----+-----------------+-----------+-----------------------+ 0x0450 means: - DIR = 0b01: this is an RX VLAN - SUBVLAN = 0b001: this is subvlan raspberrypi#1 - SWITCH_ID = 0b001: this is switch 1 (see the name "sw1p0") - PORT = 0b0000: this is port 0 (see the name "sw1p0") The driver also remembers the "1 -> 100" mapping. In the hotpath, if the sub-VLAN from the tag encodes a non-untagged frame, this mapping is used to create a VLAN hwaccel tag, with the value of 100. Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 38b5bee commit 3eaae1d

2 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎include/linux/dsa/8021q.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct dsa_8021q_crosschip_link {
2020
refcount_t refcount;
2121
};
2222

23+
#define DSA_8021Q_N_SUBVLAN 8
24+
2325
#if IS_ENABLED(CONFIG_NET_DSA_TAG_8021Q)
2426

2527
int dsa_port_setup_8021q_tagging(struct dsa_switch *ds, int index,
@@ -42,10 +44,14 @@ u16 dsa_8021q_tx_vid(struct dsa_switch *ds, int port);
4244

4345
u16 dsa_8021q_rx_vid(struct dsa_switch *ds, int port);
4446

47+
u16 dsa_8021q_rx_vid_subvlan(struct dsa_switch *ds, int port, u16 subvlan);
48+
4549
int dsa_8021q_rx_switch_id(u16 vid);
4650

4751
int dsa_8021q_rx_source_port(u16 vid);
4852

53+
u16 dsa_8021q_rx_subvlan(u16 vid);
54+
4955
bool vid_is_dsa_8021q(u16 vid);
5056

5157
#else
@@ -88,6 +94,11 @@ u16 dsa_8021q_rx_vid(struct dsa_switch *ds, int port)
8894
return 0;
8995
}
9096

97+
u16 dsa_8021q_rx_vid_subvlan(struct dsa_switch *ds, int port, u16 subvlan)
98+
{
99+
return 0;
100+
}
101+
91102
int dsa_8021q_rx_switch_id(u16 vid)
92103
{
93104
return 0;
@@ -98,6 +109,11 @@ int dsa_8021q_rx_source_port(u16 vid)
98109
return 0;
99110
}
100111

112+
u16 dsa_8021q_rx_subvlan(u16 vid)
113+
{
114+
return 0;
115+
}
116+
101117
bool vid_is_dsa_8021q(u16 vid)
102118
{
103119
return false;

‎net/dsa/tag_8021q.c‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
1919
* +-----------+-----+-----------------+-----------+-----------------------+
20-
* | DIR | RSV | SWITCH_ID | RSV | PORT |
20+
* | DIR | SVL | SWITCH_ID | SUBVLAN | PORT |
2121
* +-----------+-----+-----------------+-----------+-----------------------+
2222
*
2323
* DIR - VID[11:10]:
@@ -27,17 +27,24 @@
2727
* These values make the special VIDs of 0, 1 and 4095 to be left
2828
* unused by this coding scheme.
2929
*
30-
* RSV - VID[9]:
31-
* To be used for further expansion of SWITCH_ID or for other purposes.
32-
* Must be transmitted as zero and ignored on receive.
30+
* SVL/SUBVLAN - { VID[9], VID[5:4] }:
31+
* Sub-VLAN encoding. Valid only when DIR indicates an RX VLAN.
32+
* * 0 (0b000): Field does not encode a sub-VLAN, either because
33+
* received traffic is untagged, PVID-tagged or because a second
34+
* VLAN tag is present after this tag and not inside of it.
35+
* * 1 (0b001): Received traffic is tagged with a VID value private
36+
* to the host. This field encodes the index in the host's lookup
37+
* table through which the value of the ingress VLAN ID can be
38+
* recovered.
39+
* * 2 (0b010): Field encodes a sub-VLAN.
40+
* ...
41+
* * 7 (0b111): Field encodes a sub-VLAN.
42+
* When DIR indicates a TX VLAN, SUBVLAN must be transmitted as zero
43+
* (by the host) and ignored on receive (by the switch).
3344
*
3445
* SWITCH_ID - VID[8:6]:
3546
* Index of switch within DSA tree. Must be between 0 and 7.
3647
*
37-
* RSV - VID[5:4]:
38-
* To be used for further expansion of PORT or for other purposes.
39-
* Must be transmitted as zero and ignored on receive.
40-
*
4148
* PORT - VID[3:0]:
4249
* Index of switch port. Must be between 0 and 15.
4350
*/
@@ -54,6 +61,18 @@
5461
#define DSA_8021Q_SWITCH_ID(x) (((x) << DSA_8021Q_SWITCH_ID_SHIFT) & \
5562
DSA_8021Q_SWITCH_ID_MASK)
5663

64+
#define DSA_8021Q_SUBVLAN_HI_SHIFT 9
65+
#define DSA_8021Q_SUBVLAN_HI_MASK GENMASK(9, 9)
66+
#define DSA_8021Q_SUBVLAN_LO_SHIFT 4
67+
#define DSA_8021Q_SUBVLAN_LO_MASK GENMASK(4, 3)
68+
#define DSA_8021Q_SUBVLAN_HI(x) (((x) & GENMASK(2, 2)) >> 2)
69+
#define DSA_8021Q_SUBVLAN_LO(x) ((x) & GENMASK(1, 0))
70+
#define DSA_8021Q_SUBVLAN(x) \
71+
(((DSA_8021Q_SUBVLAN_LO(x) << DSA_8021Q_SUBVLAN_LO_SHIFT) & \
72+
DSA_8021Q_SUBVLAN_LO_MASK) | \
73+
((DSA_8021Q_SUBVLAN_HI(x) << DSA_8021Q_SUBVLAN_HI_SHIFT) & \
74+
DSA_8021Q_SUBVLAN_HI_MASK))
75+
5776
#define DSA_8021Q_PORT_SHIFT 0
5877
#define DSA_8021Q_PORT_MASK GENMASK(3, 0)
5978
#define DSA_8021Q_PORT(x) (((x) << DSA_8021Q_PORT_SHIFT) & \
@@ -79,6 +98,13 @@ u16 dsa_8021q_rx_vid(struct dsa_switch *ds, int port)
7998
}
8099
EXPORT_SYMBOL_GPL(dsa_8021q_rx_vid);
81100

101+
u16 dsa_8021q_rx_vid_subvlan(struct dsa_switch *ds, int port, u16 subvlan)
102+
{
103+
return DSA_8021Q_DIR_RX | DSA_8021Q_SWITCH_ID(ds->index) |
104+
DSA_8021Q_PORT(port) | DSA_8021Q_SUBVLAN(subvlan);
105+
}
106+
EXPORT_SYMBOL_GPL(dsa_8021q_rx_vid_subvlan);
107+
82108
/* Returns the decoded switch ID from the RX VID. */
83109
int dsa_8021q_rx_switch_id(u16 vid)
84110
{
@@ -93,6 +119,20 @@ int dsa_8021q_rx_source_port(u16 vid)
93119
}
94120
EXPORT_SYMBOL_GPL(dsa_8021q_rx_source_port);
95121

122+
/* Returns the decoded subvlan from the RX VID. */
123+
u16 dsa_8021q_rx_subvlan(u16 vid)
124+
{
125+
u16 svl_hi, svl_lo;
126+
127+
svl_hi = (vid & DSA_8021Q_SUBVLAN_HI_MASK) >>
128+
DSA_8021Q_SUBVLAN_HI_SHIFT;
129+
svl_lo = (vid & DSA_8021Q_SUBVLAN_LO_MASK) >>
130+
DSA_8021Q_SUBVLAN_LO_SHIFT;
131+
132+
return (svl_hi << 2) | svl_lo;
133+
}
134+
EXPORT_SYMBOL_GPL(dsa_8021q_rx_subvlan);
135+
96136
bool vid_is_dsa_8021q(u16 vid)
97137
{
98138
return ((vid & DSA_8021Q_DIR_MASK) == DSA_8021Q_DIR_RX ||

0 commit comments

Comments
 (0)