Skip to content

Commit db24a90

Browse files
dsaherndavem330
authored andcommitted
net: add support for phys_port_name
Similar to port id allow netdevices to specify port names and export the name via sysfs. Drivers can implement the netdevice operation to assist udev in having sane default names for the devices using the rule: $ cat /etc/udev/rules.d/80-net-setup-link.rules SUBSYSTEM=="net", ACTION=="add", ATTR{phys_port_name}!="", NAME="$attr{phys_port_name}" Use of phys_name versus phys_id was suggested-by Jiri Pirko. Signed-off-by: David Ahern <[email protected]> Acked-by: Jiri Pirko <[email protected]> Acked-by: Scott Feldman <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 56ef9c9 commit db24a90

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

Documentation/ABI/testing/sysfs-class-net

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ Description:
188188
Indicates the interface unique physical port identifier within
189189
the NIC, as a string.
190190

191+
What: /sys/class/net/<iface>/phys_port_name
192+
Date: March 2015
193+
KernelVersion: 4.0
194+
195+
Description:
196+
Indicates the interface physical port name within the NIC,
197+
as a string.
198+
191199
What: /sys/class/net/<iface>/speed
192200
Date: October 2009
193201
KernelVersion: 2.6.33

include/linux/netdevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,8 @@ struct net_device_ops {
11641164
bool new_carrier);
11651165
int (*ndo_get_phys_port_id)(struct net_device *dev,
11661166
struct netdev_phys_item_id *ppid);
1167+
int (*ndo_get_phys_port_name)(struct net_device *dev,
1168+
char *name, size_t len);
11671169
void (*ndo_add_vxlan_port)(struct net_device *dev,
11681170
sa_family_t sa_family,
11691171
__be16 port);
@@ -2947,6 +2949,8 @@ int dev_set_mac_address(struct net_device *, struct sockaddr *);
29472949
int dev_change_carrier(struct net_device *, bool new_carrier);
29482950
int dev_get_phys_port_id(struct net_device *dev,
29492951
struct netdev_phys_item_id *ppid);
2952+
int dev_get_phys_port_name(struct net_device *dev,
2953+
char *name, size_t len);
29502954
struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev);
29512955
struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
29522956
struct netdev_queue *txq, int *ret);

include/uapi/linux/if_link.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ enum {
147147
IFLA_CARRIER_CHANGES,
148148
IFLA_PHYS_SWITCH_ID,
149149
IFLA_LINK_NETNSID,
150+
IFLA_PHYS_PORT_NAME,
150151
__IFLA_MAX
151152
};
152153

net/core/dev.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5911,6 +5911,24 @@ int dev_get_phys_port_id(struct net_device *dev,
59115911
}
59125912
EXPORT_SYMBOL(dev_get_phys_port_id);
59135913

5914+
/**
5915+
* dev_get_phys_port_name - Get device physical port name
5916+
* @dev: device
5917+
* @name: port name
5918+
*
5919+
* Get device physical port name
5920+
*/
5921+
int dev_get_phys_port_name(struct net_device *dev,
5922+
char *name, size_t len)
5923+
{
5924+
const struct net_device_ops *ops = dev->netdev_ops;
5925+
5926+
if (!ops->ndo_get_phys_port_name)
5927+
return -EOPNOTSUPP;
5928+
return ops->ndo_get_phys_port_name(dev, name, len);
5929+
}
5930+
EXPORT_SYMBOL(dev_get_phys_port_name);
5931+
59145932
/**
59155933
* dev_new_index - allocate an ifindex
59165934
* @net: the applicable net namespace

net/core/net-sysfs.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,28 @@ static ssize_t phys_port_id_show(struct device *dev,
418418
}
419419
static DEVICE_ATTR_RO(phys_port_id);
420420

421+
static ssize_t phys_port_name_show(struct device *dev,
422+
struct device_attribute *attr, char *buf)
423+
{
424+
struct net_device *netdev = to_net_dev(dev);
425+
ssize_t ret = -EINVAL;
426+
427+
if (!rtnl_trylock())
428+
return restart_syscall();
429+
430+
if (dev_isalive(netdev)) {
431+
char name[IFNAMSIZ];
432+
433+
ret = dev_get_phys_port_name(netdev, name, sizeof(name));
434+
if (!ret)
435+
ret = sprintf(buf, "%s\n", name);
436+
}
437+
rtnl_unlock();
438+
439+
return ret;
440+
}
441+
static DEVICE_ATTR_RO(phys_port_name);
442+
421443
static ssize_t phys_switch_id_show(struct device *dev,
422444
struct device_attribute *attr, char *buf)
423445
{
@@ -465,6 +487,7 @@ static struct attribute *net_class_attrs[] = {
465487
&dev_attr_tx_queue_len.attr,
466488
&dev_attr_gro_flush_timeout.attr,
467489
&dev_attr_phys_port_id.attr,
490+
&dev_attr_phys_port_name.attr,
468491
&dev_attr_phys_switch_id.attr,
469492
NULL,
470493
};

net/core/rtnetlink.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,24 @@ static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
982982
return 0;
983983
}
984984

985+
static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
986+
{
987+
char name[IFNAMSIZ];
988+
int err;
989+
990+
err = dev_get_phys_port_name(dev, name, sizeof(name));
991+
if (err) {
992+
if (err == -EOPNOTSUPP)
993+
return 0;
994+
return err;
995+
}
996+
997+
if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
998+
return -EMSGSIZE;
999+
1000+
return 0;
1001+
}
1002+
9851003
static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
9861004
{
9871005
int err;
@@ -1072,6 +1090,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
10721090
if (rtnl_phys_port_id_fill(skb, dev))
10731091
goto nla_put_failure;
10741092

1093+
if (rtnl_phys_port_name_fill(skb, dev))
1094+
goto nla_put_failure;
1095+
10751096
if (rtnl_phys_switch_id_fill(skb, dev))
10761097
goto nla_put_failure;
10771098

0 commit comments

Comments
 (0)