Skip to content

Commit 061f6a5

Browse files
ffainellidavem330
authored andcommitted
net: dsa: Add ndo_vlan_rx_{add, kill}_vid implementation
In order to properly support VLAN filtering being enabled/disabled on a bridge, while having other ports being non bridge port members, we need to support the ndo_vlan_rx_{add,kill}_vid callbacks in order to make sure the non-bridge ports can continue receiving VLAN tags, even when the switch is globally configured to do ingress/egress VID checking. Since we can call dsa_port_vlan_{add,del} with a bridge_dev pointer NULL, we now need to check that in these two functions. We specifically deal with two possibly problematic cases: - creating a bridge VLAN entry while there is an existing VLAN device claiming that same VID - creating a VLAN device while there is an existing bridge VLAN entry with that VID Those are both resolved with returning -EBUSY back to user-space. Signed-off-by: Florian Fainelli <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent cc1d5bd commit 061f6a5

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

net/dsa/port.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ int dsa_port_vlan_add(struct dsa_port *dp,
291291
.vlan = vlan,
292292
};
293293

294-
if (br_vlan_enabled(dp->bridge_dev))
294+
/* Can be called from dsa_slave_port_obj_add() or
295+
* dsa_slave_vlan_rx_add_vid()
296+
*/
297+
if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
295298
return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
296299

297300
return 0;
@@ -306,10 +309,13 @@ int dsa_port_vlan_del(struct dsa_port *dp,
306309
.vlan = vlan,
307310
};
308311

309-
if (netif_is_bridge_master(vlan->obj.orig_dev))
312+
if (vlan->obj.orig_dev && netif_is_bridge_master(vlan->obj.orig_dev))
310313
return -EOPNOTSUPP;
311314

312-
if (br_vlan_enabled(dp->bridge_dev))
315+
/* Can be called from dsa_slave_port_obj_del() or
316+
* dsa_slave_vlan_rx_kill_vid()
317+
*/
318+
if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
313319
return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
314320

315321
return 0;

net/dsa/slave.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,72 @@ static int dsa_slave_get_ts_info(struct net_device *dev,
983983
return ds->ops->get_ts_info(ds, p->dp->index, ts);
984984
}
985985

986+
static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
987+
u16 vid)
988+
{
989+
struct dsa_port *dp = dsa_slave_to_port(dev);
990+
struct switchdev_obj_port_vlan vlan = {
991+
.vid_begin = vid,
992+
.vid_end = vid,
993+
/* This API only allows programming tagged, non-PVID VIDs */
994+
.flags = 0,
995+
};
996+
struct bridge_vlan_info info;
997+
int ret;
998+
999+
/* Check for a possible bridge VLAN entry now since there is no
1000+
* need to emulate the switchdev prepare + commit phase.
1001+
*/
1002+
if (dp->bridge_dev) {
1003+
/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1004+
* device, respectively the VID is not found, returning
1005+
* 0 means success, which is a failure for us here.
1006+
*/
1007+
ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1008+
if (ret == 0)
1009+
return -EBUSY;
1010+
}
1011+
1012+
ret = dsa_port_vlan_add(dp, &vlan, NULL);
1013+
if (ret == -EOPNOTSUPP)
1014+
ret = 0;
1015+
1016+
return ret;
1017+
}
1018+
1019+
static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1020+
u16 vid)
1021+
{
1022+
struct dsa_port *dp = dsa_slave_to_port(dev);
1023+
struct switchdev_obj_port_vlan vlan = {
1024+
.vid_begin = vid,
1025+
.vid_end = vid,
1026+
/* This API only allows programming tagged, non-PVID VIDs */
1027+
.flags = 0,
1028+
};
1029+
struct bridge_vlan_info info;
1030+
int ret;
1031+
1032+
/* Check for a possible bridge VLAN entry now since there is no
1033+
* need to emulate the switchdev prepare + commit phase.
1034+
*/
1035+
if (dp->bridge_dev) {
1036+
/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1037+
* device, respectively the VID is not found, returning
1038+
* 0 means success, which is a failure for us here.
1039+
*/
1040+
ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1041+
if (ret == 0)
1042+
return -EBUSY;
1043+
}
1044+
1045+
ret = dsa_port_vlan_del(dp, &vlan);
1046+
if (ret == -EOPNOTSUPP)
1047+
ret = 0;
1048+
1049+
return ret;
1050+
}
1051+
9861052
static const struct ethtool_ops dsa_slave_ethtool_ops = {
9871053
.get_drvinfo = dsa_slave_get_drvinfo,
9881054
.get_regs_len = dsa_slave_get_regs_len,
@@ -1048,6 +1114,8 @@ static const struct net_device_ops dsa_slave_netdev_ops = {
10481114
.ndo_setup_tc = dsa_slave_setup_tc,
10491115
.ndo_get_stats64 = dsa_slave_get_stats64,
10501116
.ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1117+
.ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1118+
.ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
10511119
};
10521120

10531121
static const struct switchdev_ops dsa_slave_switchdev_ops = {
@@ -1307,7 +1375,8 @@ int dsa_slave_create(struct dsa_port *port)
13071375
if (slave_dev == NULL)
13081376
return -ENOMEM;
13091377

1310-
slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1378+
slave_dev->features = master->vlan_features | NETIF_F_HW_TC |
1379+
NETIF_F_HW_VLAN_CTAG_FILTER;
13111380
slave_dev->hw_features |= NETIF_F_HW_TC;
13121381
slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
13131382
eth_hw_addr_inherit(slave_dev, master);

net/dsa/switch.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <linux/netdevice.h>
1414
#include <linux/notifier.h>
15+
#include <linux/if_vlan.h>
1516
#include <net/switchdev.h>
1617

1718
#include "dsa_priv.h"
@@ -168,6 +169,43 @@ static int dsa_switch_mdb_del(struct dsa_switch *ds,
168169
return 0;
169170
}
170171

172+
static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
173+
int vlan_dev_vid,
174+
void *arg)
175+
{
176+
struct switchdev_obj_port_vlan *vlan = arg;
177+
u16 vid;
178+
179+
for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
180+
if (vid == vlan_dev_vid)
181+
return -EBUSY;
182+
}
183+
184+
return 0;
185+
}
186+
187+
static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
188+
const struct switchdev_obj_port_vlan *vlan)
189+
{
190+
const struct dsa_port *dp = dsa_to_port(ds, port);
191+
int err = 0;
192+
193+
/* Device is not bridged, let it proceed with the VLAN device
194+
* creation.
195+
*/
196+
if (!dp->bridge_dev)
197+
return err;
198+
199+
/* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare pharse and
200+
* already checks whether there is an overlapping bridge VLAN entry
201+
* with the same VID, so here we only need to check that if we are
202+
* adding a bridge VLAN entry there is not an overlapping VLAN device
203+
* claiming that VID.
204+
*/
205+
return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
206+
(void *)vlan);
207+
}
208+
171209
static int
172210
dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
173211
const struct switchdev_obj_port_vlan *vlan,
@@ -179,6 +217,10 @@ dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
179217
return -EOPNOTSUPP;
180218

181219
for_each_set_bit(port, bitmap, ds->num_ports) {
220+
err = dsa_port_vlan_check(ds, port, vlan);
221+
if (err)
222+
return err;
223+
182224
err = ds->ops->port_vlan_prepare(ds, port, vlan);
183225
if (err)
184226
return err;

0 commit comments

Comments
 (0)