Skip to content

Commit d6abc59

Browse files
ffainellidavem330
authored andcommitted
net: Introduce ndo_get_port_parent_id()
In preparation for getting rid of switchdev_ops, create a dedicated NDO operation for getting the port's parent identifier. There are essentially two classes of drivers that need to implement getting the port's parent ID which are VF/PF drivers with a built-in switch, and pure switchdev drivers such as mlxsw, ocelot, dsa etc. We introduce a helper function: dev_get_port_parent_id() which supports recursion into the lower devices to obtain the first port's parent ID. Convert the bridge, core and ipv4 multicast routing code to check for such ndo_get_port_parent_id() and call the helper function when valid before falling back to switchdev_port_attr_get(). This will allow us to convert all relevant drivers in one go instead of having to implement both switchdev_port_attr_get() and ndo_get_port_parent_id() operations, then get rid of switchdev_port_attr_get(). Acked-by: Jiri Pirko <jiri@mellanox.com> Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> Reviewed-by: Ido Schimmel <idosch@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 47b9803 commit d6abc59

File tree

6 files changed

+91
-5
lines changed

6 files changed

+91
-5
lines changed

include/linux/netdevice.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,10 @@ struct dev_ifalias {
11881188
* not implement this, it is assumed that the hw is not able to have
11891189
* multiple net devices on single physical port.
11901190
*
1191+
* int (*ndo_get_port_parent_id)(struct net_device *dev,
1192+
* struct netdev_phys_item_id *ppid)
1193+
* Called to get the parent ID of the physical port of this device.
1194+
*
11911195
* void (*ndo_udp_tunnel_add)(struct net_device *dev,
11921196
* struct udp_tunnel_info *ti);
11931197
* Called by UDP tunnel to notify a driver about the UDP port and socket
@@ -1412,6 +1416,8 @@ struct net_device_ops {
14121416
bool new_carrier);
14131417
int (*ndo_get_phys_port_id)(struct net_device *dev,
14141418
struct netdev_phys_item_id *ppid);
1419+
int (*ndo_get_port_parent_id)(struct net_device *dev,
1420+
struct netdev_phys_item_id *ppid);
14151421
int (*ndo_get_phys_port_name)(struct net_device *dev,
14161422
char *name, size_t len);
14171423
void (*ndo_udp_tunnel_add)(struct net_device *dev,
@@ -3651,6 +3657,9 @@ int dev_get_phys_port_id(struct net_device *dev,
36513657
struct netdev_phys_item_id *ppid);
36523658
int dev_get_phys_port_name(struct net_device *dev,
36533659
char *name, size_t len);
3660+
int dev_get_port_parent_id(struct net_device *dev,
3661+
struct netdev_phys_item_id *ppid, bool recurse);
3662+
bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b);
36543663
int dev_change_proto_down(struct net_device *dev, bool proto_down);
36553664
struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again);
36563665
struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,

net/bridge/br_switchdev.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ static int br_switchdev_mark_get(struct net_bridge *br, struct net_device *dev)
1414

1515
/* dev is yet to be added to the port list. */
1616
list_for_each_entry(p, &br->port_list, list) {
17-
if (switchdev_port_same_parent_id(dev, p->dev))
17+
if (netdev_port_same_parent_id(dev, p->dev) ||
18+
switchdev_port_same_parent_id(dev, p->dev))
1819
return p->offload_fwd_mark;
1920
}
2021

@@ -23,6 +24,7 @@ static int br_switchdev_mark_get(struct net_bridge *br, struct net_device *dev)
2324

2425
int nbp_switchdev_mark_set(struct net_bridge_port *p)
2526
{
27+
const struct net_device_ops *ops = p->dev->netdev_ops;
2628
struct switchdev_attr attr = {
2729
.orig_dev = p->dev,
2830
.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
@@ -31,7 +33,10 @@ int nbp_switchdev_mark_set(struct net_bridge_port *p)
3133

3234
ASSERT_RTNL();
3335

34-
err = switchdev_port_attr_get(p->dev, &attr);
36+
if (ops->ndo_get_port_parent_id)
37+
err = dev_get_port_parent_id(p->dev, &attr.u.ppid, true);
38+
else
39+
err = switchdev_port_attr_get(p->dev, &attr);
3540
if (err) {
3641
if (err == -EOPNOTSUPP)
3742
return 0;

net/core/dev.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7877,6 +7877,63 @@ int dev_get_phys_port_name(struct net_device *dev,
78777877
}
78787878
EXPORT_SYMBOL(dev_get_phys_port_name);
78797879

7880+
/**
7881+
* dev_get_port_parent_id - Get the device's port parent identifier
7882+
* @dev: network device
7883+
* @ppid: pointer to a storage for the port's parent identifier
7884+
* @recurse: allow/disallow recursion to lower devices
7885+
*
7886+
* Get the devices's port parent identifier
7887+
*/
7888+
int dev_get_port_parent_id(struct net_device *dev,
7889+
struct netdev_phys_item_id *ppid,
7890+
bool recurse)
7891+
{
7892+
const struct net_device_ops *ops = dev->netdev_ops;
7893+
struct netdev_phys_item_id first = { };
7894+
struct net_device *lower_dev;
7895+
struct list_head *iter;
7896+
int err = -EOPNOTSUPP;
7897+
7898+
if (ops->ndo_get_port_parent_id)
7899+
return ops->ndo_get_port_parent_id(dev, ppid);
7900+
7901+
if (!recurse)
7902+
return err;
7903+
7904+
netdev_for_each_lower_dev(dev, lower_dev, iter) {
7905+
err = dev_get_port_parent_id(lower_dev, ppid, recurse);
7906+
if (err)
7907+
break;
7908+
if (!first.id_len)
7909+
first = *ppid;
7910+
else if (memcmp(&first, ppid, sizeof(*ppid)))
7911+
return -ENODATA;
7912+
}
7913+
7914+
return err;
7915+
}
7916+
EXPORT_SYMBOL(dev_get_port_parent_id);
7917+
7918+
/**
7919+
* netdev_port_same_parent_id - Indicate if two network devices have
7920+
* the same port parent identifier
7921+
* @a: first network device
7922+
* @b: second network device
7923+
*/
7924+
bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
7925+
{
7926+
struct netdev_phys_item_id a_id = { };
7927+
struct netdev_phys_item_id b_id = { };
7928+
7929+
if (dev_get_port_parent_id(a, &a_id, true) ||
7930+
dev_get_port_parent_id(b, &b_id, true))
7931+
return false;
7932+
7933+
return netdev_phys_item_id_same(&a_id, &b_id);
7934+
}
7935+
EXPORT_SYMBOL(netdev_port_same_parent_id);
7936+
78807937
/**
78817938
* dev_change_proto_down - update protocol port state information
78827939
* @dev: device

net/core/net-sysfs.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ static ssize_t phys_switch_id_show(struct device *dev,
495495
struct device_attribute *attr, char *buf)
496496
{
497497
struct net_device *netdev = to_net_dev(dev);
498+
const struct net_device_ops *ops = netdev->netdev_ops;
498499
ssize_t ret = -EINVAL;
499500

500501
if (!rtnl_trylock())
@@ -507,7 +508,11 @@ static ssize_t phys_switch_id_show(struct device *dev,
507508
.flags = SWITCHDEV_F_NO_RECURSE,
508509
};
509510

510-
ret = switchdev_port_attr_get(netdev, &attr);
511+
if (ops->ndo_get_port_parent_id)
512+
ret = dev_get_port_parent_id(netdev, &attr.u.ppid,
513+
false);
514+
else
515+
ret = switchdev_port_attr_get(netdev, &attr);
511516
if (!ret)
512517
ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
513518
attr.u.ppid.id);

net/core/rtnetlink.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,14 +1146,18 @@ static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
11461146

11471147
static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
11481148
{
1149+
const struct net_device_ops *ops = dev->netdev_ops;
11491150
int err;
11501151
struct switchdev_attr attr = {
11511152
.orig_dev = dev,
11521153
.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
11531154
.flags = SWITCHDEV_F_NO_RECURSE,
11541155
};
11551156

1156-
err = switchdev_port_attr_get(dev, &attr);
1157+
if (ops->ndo_get_port_parent_id)
1158+
err = dev_get_port_parent_id(dev, &attr.u.ppid, false);
1159+
else
1160+
err = switchdev_port_attr_get(dev, &attr);
11571161
if (err) {
11581162
if (err == -EOPNOTSUPP)
11591163
return 0;

net/ipv4/ipmr.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
837837
static int vif_add(struct net *net, struct mr_table *mrt,
838838
struct vifctl *vifc, int mrtsock)
839839
{
840+
const struct net_device_ops *ops;
840841
int vifi = vifc->vifc_vifi;
841842
struct switchdev_attr attr = {
842843
.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
@@ -920,7 +921,12 @@ static int vif_add(struct net *net, struct mr_table *mrt,
920921
(VIFF_TUNNEL | VIFF_REGISTER));
921922

922923
attr.orig_dev = dev;
923-
if (!switchdev_port_attr_get(dev, &attr)) {
924+
ops = dev->netdev_ops;
925+
if (ops->ndo_get_port_parent_id &&
926+
!dev_get_port_parent_id(dev, &attr.u.ppid, true)) {
927+
memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
928+
v->dev_parent_id.id_len = attr.u.ppid.id_len;
929+
} else if (!switchdev_port_attr_get(dev, &attr)) {
924930
memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
925931
v->dev_parent_id.id_len = attr.u.ppid.id_len;
926932
} else {

0 commit comments

Comments
 (0)