static ssize_t phys_switch_id_show(struct device *dev, struct device_attribute *attr, char *buf) { struct net_device *netdev = to_net_dev(dev); ssize_t ret = -EINVAL; if (!rtnl_trylock()) return restart_syscall(); if (dev_isalive(netdev)) { struct switchdev_attr attr = { .orig_dev = netdev, .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, .flags = SWITCHDEV_F_NO_RECURSE, }; ret = switchdev_port_attr_get(netdev, &attr); if (!ret) ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len, attr.u.ppid.id); } rtnl_unlock(); return ret; }
/** * switchdev_port_attr_get - Get port attribute * * @dev: port device * @attr: attribute to get */ int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr) { const struct switchdev_ops *ops = dev->switchdev_ops; struct net_device *lower_dev; struct list_head *iter; struct switchdev_attr first = { .id = SWITCHDEV_ATTR_UNDEFINED }; int err = -EOPNOTSUPP; if (ops && ops->switchdev_port_attr_get) return ops->switchdev_port_attr_get(dev, attr); if (attr->flags & SWITCHDEV_F_NO_RECURSE) return err; /* Switch device port(s) may be stacked under * bond/team/vlan dev, so recurse down to get attr on * each port. Return -ENODATA if attr values don't * compare across ports. */ netdev_for_each_lower_dev(dev, lower_dev, iter) { err = switchdev_port_attr_get(lower_dev, attr); if (err) break; if (first.id == SWITCHDEV_ATTR_UNDEFINED) first = *attr; else if (memcmp(&first, attr, sizeof(*attr))) return -ENODATA; }